iOS单例的实现

OC版单例

    
+ (instancetype) sharedInstance { 
static id sharedInstance = nil; 
static dispatch_once_t onceToken;                     
dispatch_once(&onceToken, ^{ sharedInstance = [[self alloc] init]; }); 

 return sharedInstance;
}
  • Swift版
      static let sharedInstance : <#SingletonClass#> = <#SingletonClass#>()

你可能感兴趣的:(iOS单例的实现)