在項目中,單例用得比較多,當然代碼基本上網上都有。大多數情況,也基本上自己寫自己用,所以不會出現單例還用copy,alloc init
之類的問題。
這里有一篇文章,感覺很不錯,照著寫了一下,基本上可以,當然稍有不同,所以備忘一下。
調用接口
- 名字可以統一為
sharedInstance
,放在h
文件中:
+ (instancetype)sharedInstance;
靜態常量,不需要全局的,寫成局部變量就可以。
static
類型,全局和局部差別不大,所以寫成局部變量就可以。初始化用
allocWithZone
,不用alloc
;單例的類型,不需要指定具體的類,只需要通用的
id
就可以了。
+ (instancetype)sharedInstance {
static id instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[super allocWithZone:NULL] init];
});
return instance;
}
重寫方法
重寫
allocWithZone
,讓alloc init
也返回單例。不用出現具體的類名,用
[self class]
替代。
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
return [[self class] sharedInstance];
}
- 在
ARC
下,沒有copyWithZone
函數,這個可以從NSObject.h
中看到
+ (id)copyWithZone:(struct _NSZone *)zone OBJC_ARC_UNAVAILABLE;
+ (id)mutableCopyWithZone:(struct _NSZone *)zone OBJC_ARC_UNAVAILABLE;
-
copy
和mutableCopy
也需要重寫一下,代碼也是一樣的
- (id)copy {
return [[self class] sharedInstance];
}
- (id)mutableCopy {
return [[self class] sharedInstance];
}
例子代碼
這樣一來,所有的單例就可以用完全相同的代碼來表示了:
+ (instancetype)sharedInstance {
static id instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[super allocWithZone:NULL] init];
});
return instance;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
return [[self class] sharedInstance];
}
- (id)copy {
return [[self class] sharedInstance];
}
- (id)mutableCopy {
return [[self class] sharedInstance];
}
測試代碼
- 使用的時候用類名,也就是具體的文件名字。
XXXWebViewManager* obj1 = [XXXWebViewManager sharedInstance] ;
NSLog(@"obj1 = %@.", obj1);
XXXWebViewManager* obj2 = [XXXWebViewManager sharedInstance] ;
NSLog(@"obj2 = %@.", obj2);
XXXWebViewManager* obj3 = [[XXXWebViewManager alloc] init];
NSLog(@"obj3 = %@.", obj3);
XXXWebViewManager* obj4 = [[XXXWebViewManager alloc] init];
NSLog(@"obj4 = %@.", [obj4 copy]);
XXXWebViewManager* obj5 = [[XXXWebViewManager alloc] init];
NSLog(@"obj4 = %@.", [obj5 mutableCopy]);
- 輸出結果,所有對象的地址都是一樣的,也不會崩潰。如果沒有重寫
copy
或者mutableCopy
,用到的時候會崩潰。
2018-09-30 13:38:12.659190+0800 Wallet[42247:11822152] +[XXXWebView showWithController:url:] 第18行
obj1 = <XXXWebViewManager: 0x600001ab1630>.
2018-09-30 13:38:12.659417+0800 Wallet[42247:11822152] +[XXXWebView showWithController:url:] 第20行
obj2 = < XXXWebViewManager: 0x600001ab1630>.
2018-09-30 13:38:12.659550+0800 Wallet[42247:11822152] +[XXXWebView showWithController:url:] 第22行
obj3 = < XXXWebViewManager: 0x600001ab1630>.
2018-09-30 13:38:12.659635+0800 Wallet[42247:11822152] +[XXXWebView showWithController:url:] 第24行
obj4 = < XXXWebViewManager: 0x600001ab1630>.
2018-09-30 13:38:14.272292+0800 Wallet[42247:11822152] +[XXXWebView showWithController:url:] 第26行
obj5 = < XXXWebViewManager: 0x600001ab1630>.
宏替換
既然所有的單例都完全一樣的代碼,那么就考慮用宏來進行統一的替換。
- 頭文件中的接口名字要對起來,所以要提供一個單例的
interface
#define kSingletonInterface + (instancetype)sharedInstance;
- 實現文件中就是上面貼的所有單例完全一樣的樣例代碼:
#define kSingletonImplementation \
\
+ (instancetype)sharedInstance { \
static id instance = nil; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
instance = [[super allocWithZone:NULL] init]; \
}); \
return instance; \
} \
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone { \
return [[self class] sharedInstance]; \
} \
\
- (id)copy { \
return [[self class] sharedInstance]; \
} \
\
- (id)mutableCopy { \
return [[self class] sharedInstance]; \
}
使用宏
- 頭文件中是這樣的:
@interface XXXWebViewManager : NSObject
kSingletonInterface
@end
- 實現文件中是這樣的:
#import "XXXWebViewManager.h"
@implementation XXXWebViewManager
kSingletonImplementation
@end
不同單例,只要換不同的類名和文件名就可以了。
使用單例還是像上面的測試代碼一樣,用
[類名 sharedInstance]
來獲取單例;
參考文章
下面兩篇文章都不錯,值得認真看看,推薦