轉(zhuǎn)載自http://www.itdecent.cn/p/85618bcd4fee?utm_source=tuicool&utm_medium=referral
感謝Jingege(http://www.itdecent.cn/users/5dffd76b9caf)
#import"Singleton.h"@implementationSingletonstaticSingleton* _instance = nil;
+(instancetype) shareInstance
{static dispatch_once_t onceToken ;
dispatch_once(&onceToken, ^{
_instance = [[superallocWithZone:NULL] init] ;
}) ;return_instance ;
}
+(id) allocWithZone:(struct _NSZone *)zone
{return[Singleton shareInstance] ;
}
-(id) copyWithZone:(struct _NSZone *)zone
{return[Singleton shareInstance] ;
}@end
我就問一個問題,如果我想銷毀一個單列對象,需要怎么做?
1. 必須把static dispatch_once_t onceToken; 這個拿到函數(shù)體外,成為全局的.
2. +(void)attempDealloc{
onceToken = 0; // 只有置成0,GCD才會認(rèn)為它從未執(zhí)行過.它默認(rèn)為0.這樣才能保證下次再次調(diào)用shareInstance的時候,再次創(chuàng)建對象.
[_instance release];
_instance = nil;
}