iOS 最穩的單例寫法

@implementation MyClass

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    static MyClass *share;
    dispatch_once(&onceToken, ^{
        share = [[super allocWithZone:NULL] init];
    });
    return share;
}

+ (instancetype)shareInstance{
    return [[self alloc] init];
}

@end
複製代碼
相關文章
相關標籤/搜索