Objective-C (iOS) is strictly implemented in the singleton mode. objective-cios

Source: Internet
Author: User

Objective-C (iOS) is strictly implemented in the singleton mode. objective-cios

Note: the ownership of this article is owned by the author. For more information, see the source.

If you want to have only one class object in an application, you can use the singleton mode, the Singleton mode is easier to implement in C ++ (you only need to declare the constructor as private), while in Objective-C, objects can be generated through alloc of NSObject, therefore, you need to write some additional code to ensure the uniqueness of the object. Considering that currently, writing iOS APP code is almost always in the ARC mode, and GCD has been used up, therefore, this article provides an ARC version that uses GCD technology to implement the strict Singleton mode. The specific code is as follows. All the points of attention are written in the Notes:

1 Singleton. h 2 @ interface Singleton: NSObject 3 @ property (nonatomic, strong) NSString * name; 4 + (Singleton *) defaultManager; 5 @ end 6 7 8 Singleton. m 9 @ implementation Singleton10 // The static Instance Object of the Singleton10 class. Because the object needs to be unique, it can only be static type 11 static Singleton * defaultManager = nil; 12 13/** the unique interface in singleton mode. The dispatch_once function is only executed once in an application, and dispatch_once can ensure thread safety 14 */15 + (Singleton *) defaultManager16 {17 static dispatch_once_t token; 18 dispatch_once (& token, ^ {19 if (defaultManager = nil) 20 {21 defamanager manager = [[self alloc] init]; 22} 23}); 24 return defamanager manager; 25} 26 27/** overwrite this method to ensure the uniqueness of the object when the user creates an object through [[Singleton alloc] init]. The alloc method calls this method, however, the default zone parameter is nil. Because this class overrides the allocWithZone method, the memory can only be allocated through its parent class, that is, [super allocWithZone: zone] 28 */29 + (id) allocWithZone :( struct _ NSZone *) zone30 {31 static dispatch_once_t token; 32 dispatch_once (& token, ^ {33 if (defaultManager = nil) 34 {35 defaultManager = [super allocWithZone: zone]; 36} 37}); 38 return defamanager manager; 39} 40 // custom initialization method. In this example, only the name attribute is 41-(instancetype) init42 {43 self = [super init]; 44 if (self) 45 {46 self. name = @ "Singleton"; 47} 48 return self; 49} 50 51 // overwrite this method to ensure the uniqueness of the object 52-(id) when the user generates an object through the copy method) copy53 {54 return self; 55} 56 57 // overwrite this method to ensure the uniqueness of the object 58-(id) mutableCopy59 {60 return self; 61} 62 // custom description for log printing 63-(NSString *) description64 {65 return [NSString stringWithFormat: @ "memeory address: % p, property name: % @ ", self, self. name]; 66}

The test code is as follows:

1 Singleton * defaultManagerSingleton = [Singleton defaultManager]; 2 NSLog (@ "defaultManagerSingleton: \ n % @", listener); 3 Singleton * allocSingleton = [[Singleton alloc] init]; 4 NSLog (@ "allocSingleton: \ n % @", allocSingleton); 5 Singleton * copySingleton = [allocSingleton copy]; 6 NSLog (@ "copySingleton: \ n % @", copySingleton); 7 Singleton * mutebleCopySingleton = [allocSingleton mutableCopy]; 8 NSLog (@ "Usage: \ n % @", mutebleCopySingleton); 9 10 // print the result 21:48:34. 722 Singleton [1941: 214584] defaultManagerSingleton: 12 memeory address: 0x7fa6d1591530, property name: Singleton13 21:48:34. 727 Singleton [1941: 214584] allocSingleton: 14 memeory address: 0x7fa6d1591530, property name: Singleton15 21:48:34. 727 Singleton [1941: 214584] copySingleton: 16 memeory address: 0x7fa6d1591530, property name: Singleton17 21:48:34. 727 Singleton [1941: 214584] mutebleCopySingleton: 18 memeory address: 0x7fa6d1591530, property name: Singleton

The output shows that the object addresses generated by [Singleton ultultmanager], [Singleton alloc] init], [allocSingleton copy], and [allocSingleton mutableCopy] are 0x7fa6d1591530, this means that the object is the same, and the strict Singleton mode is implemented. In addition, GCD is thread-safe, so the uniqueness of the object can be guaranteed in multiple threads.

In addition, when I learned the Objective-C Singleton mode, many people on the internet borrowed the official implementation method from Apple, but I never found the official Sample code, if you know, please send me the website. Thank you ~

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.