美文网首页
实现一个单例

实现一个单例

作者: 小小旭 | 来源:发表于2020-05-19 19:43 被阅读0次

    话不多说,直接撸代码
    //================== LogManager.h ==================

    @interface LogManager : NSObject
    
    +(instancetype)sharedInstance;
    
    - (void)printLog:(NSString *)logMessage;
    
    - (void)uploadLog:(NSString *)logMessage;
    
    @end
    

    //================== LogManager.m ==================

    @implementation LogManager
    
    static LogManager* _sharedInstance = nil;
    
    +(instancetype)sharedInstance
    {
        static dispatch_once_t onceToken ;
        dispatch_once(&onceToken, ^{
            _sharedInstance = [[super allocWithZone:NULL] init] ;
        }) ;
        return _sharedInstance ;
    }
    
    +(id)allocWithZone:(struct _NSZone *)zone
    {
        return [LogManager sharedInstance] ;
    }
    
    -(id)copyWithZone:(struct _NSZone *)zone
    {
        return [LogManager sharedInstance];
    }
    
    -(id)mutableCopyWithZone:(NSZone *)zone
    {
        return [LogManager sharedInstance];
    }
    
    - (void)printLog:(NSString *)logMessage{
        //print logMessage
    }
    
    - (void)uploadLog:(NSString *)logMessage{
        //upload logMessage
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:实现一个单例

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