小编Jud*_*you的帖子

objc中的单例模式,如何保持init私有?

如何确保用户不调用init,而客户端应该调用sharedSingleton来获取共享实例.

@synthesize delegate;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }

    return self;
}

+ (LoginController *)sharedSingleton
{
    static LoginController *sharedSingleton;

    @synchronized(self)
    {
        if (!sharedSingleton)
            sharedSingleton = [[LoginController alloc] init];
        CdtMiscRegisterConnectionChangeListenerObjc(test_ConnectionChangeListenerCallback);
        return sharedSingleton;
    }
}
Run Code Online (Sandbox Code Playgroud)

singleton objective-c

3
推荐指数
2
解决办法
4113
查看次数

标签 统计

objective-c ×1

singleton ×1