单例

作者: 阶梯 | 来源:发表于2017-09-11 17:53 被阅读4次

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

    @interface DJTools : NSObject<NSCopying>
    
    +(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];
    }
    
    
    

    相关文章

      网友评论

          本文标题:单例

          本文链接:https://www.haomeiwen.com/subject/rfvfsxtx.html