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