单例

.h中,遵循协议,暴露方法

@interface DJTools : NSObject

+(DJTools *)shareDJTools;

-(void)showLogInfo;

@end

.m中

static DJTools *djTools = nil;

+(DJTools *)shareDJTools{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        djTools = [[super allocWithZone:NULL] init];
    });
    return djTools;
}


-(void)showLogInfo{
    NSLog(@"我写了单例");
}

+(id)allocWithZone:(struct _NSZone *)zone{
    return [DJTools shareDJTools];
}

-(id)copyWithZone:(NSZone *)zone{
    return [DJTools shareDJTools];
}


你可能感兴趣的:(单例)