轉載自http://www.reibang.com/p/85618bcd4fee?utm_source=tuicool&utm_medium=referral
感謝Jingege(http://www.reibang.com/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; 這個拿到函數體外,成為全局的.
2. +(void)attempDealloc{
onceToken = 0; // 只有置成0,GCD才會認為它從未執(zhí)行過.它默認為0.這樣才能保證下次再次調用shareInstance的時候,再次創(chuàng)建對象.
[_instance release];
_instance = nil;
}