A singleton is a design pattern commonly used in iOS development, and is often applied to the transfer of values between controllers. Convenient, efficient and globally versatile.
The design of the singleton model is divided into arc and MRC.
ARC:
Way One
1. Create a class that inherits from NSObject.
2. Declare the class method in the. h file of this class:
+ (instancetype) sharedinstance;
2. The following methods are implemented in the. m file for this class:
static ID instance;
+ (Instancetype) sharedinstance
{
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
instance = [[Super alloc] init];
});
return instance;
}
+ (Instancetype) Allocwithzone: (struct _nszone *) zone
{
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
instance = [Super Allocwithzone:zone];
});
return instance;
}
Way two:
iOS development--a few design methods of single case