1+(instancetype) shareduserloginmodel{2 3 StaticYlwuserloginmodel *instance;4 5 Staticdispatch_once_t Oncetoken;6 7Dispatch_once (&oncetoken, ^{8 9Instance =[[Ylwuserloginmodel alloc]init];Ten One }); A returninstance; - -}
The benefits of this notation are:
1. Thread safety.
2. Meet the requirements of the static analyzer.
3. Compatible with ARC
Summary: 1. This method can be used to create a singleton or some initialization action, to ensure its uniqueness. 2. This method is thread-safe, so be sure to use it boldly in a child thread. (If your dispatch_once_t *predicate object must be a global or static object.) This is important, and if this is not guaranteed, there is no guarantee that the method will only be executed once. )
Transferred from: http://www.lrdup.net/archives/1034
Recommended wording for iOS singleton mode (reprint)