单例的全部实现


static Singleton *sharedSingleton = nil;

+(Singleton *)sharedInstance{
    if (sharedSingleton == nil) {
        sharedSingleton = [[super allocWithZone:NULL] init];
    }
    return sharedSingleton;
}
+(id)allocWithZone:(NSZone *)zone
{
    return [[self sharedInstance] retain];
}
-(id)copyWithZone:(NSZone *)zone
{
    return self;
}
-(id)retain
{
    return self;
}
-(NSUInteger)retainCount
{
    return NSUIntegerMax;//表示此对象不能释放
}
//下面的oneway也是多线程操作,记住
-(oneway void)release{
    //do nothing
}
-(id)autorelease
{
    return self;
}

 

posted @ 2012-12-26 09:54  白条围巾  阅读(155)  评论(0编辑  收藏  举报