• iOS使用宏写单例


    本文只介绍ARC情况下的单例

    过去一直背不下来单例如何写,就是知道这么回事,也知道通过宏来写单例,但是一直记不住,今天就来记录一下

    - (void)viewDidLoad {
        [super viewDidLoad];
        SIPerson *person = [[SIPerson alloc] init];    NSLog(@"%@",person);
    
        SIPerson *person1 = [[SIPerson alloc] init];    NSLog(@"%@",person1);
    
    }

    创建person,打印,实际上是2个对象。没毛病.

    创建方法

    #import "SIPerson.h"
    
    static SIPerson *instance_ = nil;
    @implementation SIPerson
    ///方法1,快速创建对象
    + (instancetype)sharedInstance{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance_ = [[self alloc] init];
        });
        return instance_;
    }
    
    ///方法2.这个方法一定要有,就是alloc] init]方法,一定会调用这个方法
    + (instancetype)allocWithZone:(struct _NSZone *)zone{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance_ = [super allocWithZone:zone];
        });
        return instance_;
    }
    @end
    //此处还应该有一个+ copy方法,因为可能是copy,那么有可能是生成新的方法

    方法12都要实现,才能是单例。假如方法2没有实现,通过sharedInstance实现的确实是一个单例,但是通过alloc] init]有生成了另一个对象

    2016-09-17 14:17:45.086 SharedInstance[9158:611161] 2016-09-17 14:17:45.087 SharedInstance[9158:611161]

    如果你的对象将来可能还要调用copy(应该声明协议),那么你应该还要实现一个方法

    - (id)copyWithZone:(NSZone *)zone{    
        return instance_;
    }

    copy的时候,一般是生成了一个新的对象,所以不是单例了,但是用的时候比较少,不是特别需要的,可以不实现这个方法,为毛要这样去写?因为他是对象方法,instance_里面有值

        [super viewDidLoad];
        SIPerson *person = [SIPerson sharedInstance];
        NSLog(@"%@",person);
    
        SIPerson *person1 = [[SIPerson alloc] init];
        NSLog(@"%@",person1);    
    
        SIPerson *person2 = [person1 copy];
        NSLog(@"%@",person2);

    结果如下

    2016-09-17 14:24:10.555 SharedInstance[9199:615987] 2016-09-17 14:24:10.555 SharedInstance[9199:615987] 2016-09-17 14:24:10.556 SharedInstance[9199:615987]

    1648725-d7fb433d54af54df.jpg

    /**
     *  在.h文件中定义的宏,arc
     *
     *  SISingletonH(name) 这个是宏
     *  + (instancetype)shared##name;这个是被代替的方法, ##代表着shared+name 高度定制化
     * 在外边我们使用 “SISingletonH(gege)” 那么在.h文件中,定义了一个方法"+ (instancetype)sharedgege",所以,第一个字母要大写
     *
     *  @return 一个搞定好的方法名
     */
    #define SISingletonH(name) + (instancetype)shared##name;
    
    
    /**
     *  在.m文件中处理好的宏 arc
     *
     *  SISingletonM(name) 这个是宏,因为是多行的东西,所以每行后面都有一个"",最后一行除外,
     * 之所以还要传递一个“name”,是因为有个方法要命名"+ (instancetype)shared##name"
     *  @return 单利
     */
    #define SISingletonM(name) 
    static SIPerson *instance_ = nil;
    + (instancetype)shared##name{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance_ = [[self alloc] init];
        });
        return instance_;
    }
    + (instancetype)allocWithZone:(struct _NSZone *)zone{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance_ = [super allocWithZone:zone];
        });
        return instance_;
    }
    - (id)copyWithZone:(NSZone *)zone{
        return instance_;
    }

    实际使用

    //.h文件SISingletonH(Default)
    //.m文件SISingletonM(Default)

    都是一句话,都没有符号(定义的时候就给了符号),就这么简单


    在实际使用的时候

     [super viewDidLoad];
        SIPerson *person = [SIPerson sharedDefault];
        NSLog(@"%@",person);
    
        SIPerson *person1 = [[SIPerson alloc] init];
        NSLog(@"%@",person1);
    
        SIPerson *person2 = [person1 copy];
        NSLog(@"%@",person2);
    
    //打印结果
    2016-09-17 14:56:39.508 SharedInstance[9292:633076] 2016-09-17 14:56:39.508 SharedInstance[9292:633076] 2016-09-17 14:56:39.508 SharedInstance[9292:633076]

    拿来就可以使用的文件地址

    简单说一下如何定义swift版本的单例,正常写,没研究过单例

    1648725-4ca6d68628cf219b.png


    1648725-4631c7792e4b4f8e.jpg

  • 相关阅读:
    css 中 overflow: hidden清楚浮动的真正原因
    Get、Post请求中文乱码问题有效解决方法
    安装PostgreSQL和卸载PostgreSQL
    PostgresSql日期字段比较大小
    关于多线程编程的一点思考
    Python 虚拟环境 pyenv、venv(pyvenv)、virtualenv之间的区别
    python常用命令
    python中各种文件打开模式a w r
    python-安装
    day1-安装配置
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5908219.html
Copyright © 2020-2023  润新知