Objective-c Single-Case mode

Source: Internet
Author: User

A singleton class is a special class in which only one object of that class exists in a process type, and only one object appears in an iOS app. This design pattern is used in many parts of the system framework, such as Nsfilemanager, uiapplication and so on.

    • In the ARC environment, the interface file is:
//// DVISingleton.h////Copyright (c) 2014 Changsha Camp David Education. All rights reserved. //#import <Foundation/Foundation.h>@interface Dvisingleton: nsobject+ (instancetype ) Sharedsingleton; @end          

Implementation file:

//Dvisingleton.m//Copyright (c) 2014 Changsha Camp David Education. All rights reserved.//#import"DVISingleton.h"@implementationdvisingleton+ (instancetype) sharedsingleton{static Dvisingleton *sharedobject =NilThread safe, only allowed to execute sequentiallyStaticdispatch_once_t Oncetoken;Dispatch_once (&oncetoken, ^{Create an object using the Allocwithzone: Method of the parent class sharedobject = [[Super Allocwithzone:NULL] init]; });return sharedobject;} - (ID) init{if (self = [super Init]) {} return  Self;} + (id) Allocwithzone: (struct _nszone *) zone{return [self Sharedsingleton];} -(id) copy{ return void) dealloc{}  @end          
    • Implementation files in non-ARC environments:
#import"DVISingleton.h"@implementationdvisingleton+ (instancetype) sharedsingleton{static Dvisingleton *sharedobject =NilThread safe, only allowed to execute sequentiallyStaticdispatch_once_t Oncetoken;Dispatch_once (&oncetoken, ^{Create an object using the Allocwithzone: Method of the parent class sharedobject = [[Super Allocwithzone:NULL] init]; });return sharedobject;} + (ID) Allocwithzone: (Nszone *) Zone {return [[Self Sharedsingleton] retain];} - (ID) Copywithzone: (Nszone *) Zone {ReturnSelf;} - (ID) Retain {ReturnSelf;} - (unsigned) Retaincount {return Uint_max;Denotes an object, cannot be released}-(oneway void) Release { //never release}-(id) autorelease {return self;} -(id) init { if (self = [super Init]) {} return  Self;} -(void) dealloc {[ super Dealloc];}  @end          

Objective-c Singleton mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.