我不明白使用NSAssert的+alloc
,當+alloc
從+sharedGameManager
叫,靜態_sharedGameManager
變量是nil
(所以NSAssert
應停止執行首次[self alloc] init]
叫...)Singleton中的NSAssert:爲什麼此代碼有效?
+(GameManager*)sharedGameManager {
@synchronized([GameManager class])
{
if(!_sharedGameManager)
[[self alloc] init];
return _sharedGameManager;
}
return nil;
}
+(id)alloc
{
@synchronized ([GameManager class])
{
NSAssert(_sharedGameManager == nil,
@"Attempted to allocated a second instance of the Game Manager singleton");
_sharedGameManager = [super alloc];
return _sharedGameManager;
}
return nil;
}
感謝您的回答
哦,是的,我不明白爲什麼我確定它是相反的:)謝謝你的答案! – Paul 2012-04-19 23:25:07