创建安全的单例

 

 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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-22
  • 2021-08-18
  • 2021-09-30
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-16
  • 2022-12-23
  • 2022-12-23
  • 2021-08-14
相关资源
相似解决方案