美文网首页
OC 单例模式

OC 单例模式

作者: CaptainRoy | 来源:发表于2018-07-24 11:12 被阅读2次
    #import <Foundation/Foundation.h>
    
    @interface Singleton : NSObject
    
    +(Singleton *)shareInstance;
    
    @end
    
    #import "Singleton.h"
    
    @implementation Singleton
    
    +(Singleton *)shareInstance
    {
        static Singleton *singleton = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            singleton = [[super allocWithZone:NULL] init];
        });
        return singleton;
    }
    
    + (instancetype)allocWithZone:(struct _NSZone *)zone
    {
        return [Singleton shareInstance];
    }
    
    -(instancetype)copyWithZone:(struct _NSZone *)zone
    {
        return [Singleton shareInstance];
    }
    
    -(instancetype)mutableCopyWithZone:(struct _NSZone *)zone
    {
        return [Singleton shareInstance];
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:OC 单例模式

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