#import "Singleton.h"
@implementation Singleton
static Singleton* _instance = nil;
+(instancetype) shareInstance
{
static dispatch_once_t onceToken ;
dispatch_once(&onceToken, ^{
_instance = [[super allocWithZone:NULL] init] ;
}) ;
return _instance ;
}
+(id) allocWithZone:(struct _NSZone *)zone
{
return [Singleton shareInstance] ;
}
-(id) copyWithZone:(struct _NSZone *)zone
{
return [Singleton shareInstance] ;
}
@end
Singleton* obj1 = [Singleton shareInstance] ;
NSLog(@"obj1 = %@.", obj1) ;
Singleton* obj2 = [Singleton shareInstance] ;
NSLog(@"obj2 = %@.", obj2) ;
Singleton* obj3 = [[Singleton alloc] init] ;
NSLog(@"obj3 = %@.", obj3) ;
2017-03-06 15:59:51.790 Sqlite3[3068:1369867] obj1 = <SqliteTool: 0x608000010660>.
2017-03-06 15:59:51.790 Sqlite3[3068:1369867] obj2 = <SqliteTool: 0x608000010660>.
2017-03-06 15:59:51.791 Sqlite3[3068:1369867] obj3 = <SqliteTool: 0x608000010660>.