First of all, the function of the single class can be implemented, but also in the project is often used. Because the single case is the global where to use the direct call on the line is very convenient and simple, generally we can use a single example to the user information storage, and then a single example can be made shopping cart pages and so on. Of course, the single biggest advantage of personal feeling is that the data storage and reading is very convenient, you can solve the problem of transmission between pages. Simple how to use a single example of data transmission bar, the need to define the information is a property, when the need to store a direct call to store on the line, to use the time to pull it out of use on the line here do not do too much description.
The following is a simple demo to detail the implementation process (engineering using non-arc mode) XCODE5 only need to import the head file supporting files inside. H Precompiled header file can be accessed globally any class since xcode6 canceled this file so need when you need to use You need to import the next
#import "SuperMan.h" static Superman *superman = nil;//define global static variable @implementation superman//Override Alloc method Block creation method (when the Alloc method is called The default is to go allocwithzone this method so just block Allocwithzone method) + (ID) allocwithzone: (struct _nszone *) zone{ if (!superman) { Superman = [Super allocwithzone:zone];//If there is no instance for the parent class to create a return Superman; } return nil;} + (Superman *) Sharesuperman //Define a class method for access (convenient construction) { if (!superman) { Superman = [[Superman alloc]init];//] If the instance does not exist for creation } return Superman;} -(ID) retain//(non-arc valid) blocking retain method A Single instance cannot be held by another object { return self;} -(OneWay void) Release//(non-arc valid) blocking release method (Let Singleton not be released) { }-(nsuinteger) Retaincount //(non-arc valid) { return Nsuintegermax;} Block deep copy (both copy and mutablecopy can be deep copy but they end up needing to call the Copywithzone method to block it directly)-(ID) Copywithzone: (struct _nszone *) zone{ return self;} @end
The above are personal understanding, I am also a rookie, there is no understanding of the place I hope you point out, but also hope to be able to help you a certain!! Thank you!
A summary of the creation and use techniques of the iOS single-instance class