zoukankan      html  css  js  c++  java
  • 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] <SIPerson: 0x7f9ab260bfe0>
    2016-09-17 14:17:45.087 SharedInstance[9158:611161] <SIPerson: 0x7f9ab2686da0>

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

    - (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] <SIPerson: 0x7f8302d07050>
    2016-09-17 14:24:10.555 SharedInstance[9199:615987] <SIPerson: 0x7f8302d07050>
    2016-09-17 14:24:10.556 SharedInstance[9199:615987] <SIPerson: 0x7f8302d07050>

    新建文件的步骤

    /**
     *  在.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 id 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_;
    }

    实际使用

    #import <Foundation/Foundation.h>
    #import "SISingleton.h"
    @interface SIPerson : NSObject
    @property(copy,nonatomic) NSString *name;
    @property(copy,nonatomic) NSString *num;
    //整个就是宏,在.h文件就这样使用
    SISingletonH(Default)
    @end
    #import "SIPerson.h"
    
    @implementation SIPerson
    
    //整个就是宏,在.m文件就这样使用
    SISingletonM(Default)
    
    @end

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


    在实际使用的时候

        [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] <SIPerson: 0x7ff85a51d250>
    2016-09-17 14:56:39.508 SharedInstance[9292:633076] <SIPerson: 0x7ff85a51d250>
    2016-09-17 14:56:39.508 SharedInstance[9292:633076] <SIPerson: 0x7ff85a51d250>

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

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

    就这样~


    摘抄

  • 相关阅读:
    mysql int类型 int(11) 和int(2)区别
    mysql 用户 登陆 权限相关
    【转】ext4+delalloc造成单次写延迟增加的分析
    write 系统调用耗时长的原因
    【转】通过blktrace, debugfs分析磁盘IO
    win7 wifi热点
    【SystemTap】 Linux下安装使用SystemTap源码安装SystemTap
    pdflush进程介绍与优化【转】
    How to find per-process I/O statistics on Linux
    oom_killer
  • 原文地址:https://www.cnblogs.com/luqinbin/p/5908367.html
Copyright © 2011-2022 走看看