iOS创建安全的单例

创建安全的单例

 

 1 #import "Singleton.h"
 2 
 3 @implementation Singleton
 4 static Singleton* _instance = nil;
 5 +(instancetype) shareInstance
 6 {
 7     static dispatch_once_t onceToken ;
 8     dispatch_once(&onceToken, ^{
 9     _instance = [[super allocWithZone:NULL] init] ;
10 }) ;
11 return _instance ;
12 }
13 
14 +(id) allocWithZone:(struct _NSZone *)zone
15 {
16     return [Singleton shareInstance] ;
17 }
18 
19 -(id) copyWithZone:(struct _NSZone *)zone
20 {
21     return [Singleton shareInstance] ;
22 }
23 @end

 

posted @ 2016-11-02 18:34  周希  阅读(281)  评论(0编辑  收藏  举报