1、苹果官方文档中的写法
1 2 3 4 5 6 | static AccountManager *DefaultManager = nil; + (AccountManager *)defaultManager { if (!DefaultManager) DefaultManager = [[self allocWithZone:NULL] init]; return DefaultManager; } |
2、iOS4之后有了另外一种写法:
1 2 3 4 5 6 7 8 9 | + (AccountManager *)sharedManager { static AccountManager *sharedAccountManagerInstance = nil; static dispatch_once_t predicate; dispatch_once(&predicate, ^{ sharedAccountManagerInstance = [[self alloc] init]; }); return sharedAccountManagerInstance; } |
3、该写法来自 objcolumnist,文中提到,该写法具有以下几个特性:[……]