星空网 > 软件开发 > 操作系统

iOS 开发中的单例

  

  在iOS开发中经常会用到单例,比如每个iOS程序本身就是一个单例,在比如进行个人偏好设置存储的时候用的也是一个单例。那我们如何自己来写一个单例类呢,用自己的单例对象呢?下面是我写的一个单例的头文件里的代码,这个文件主要是一些宏。使用步骤写的都很详细,ARC或MRC都可以使用。

 

//  Singleton.h

//  单例的宏

/*

 使用方法

        1:包含这个头文件

        2:在.h文件总包含 singleton_h(name) 里面的name是你想要生成单例对象时的名字

        3:在.m文件中包含 singleton_m(name) 里面的name是你想要生成单例对象时的名字

           注意2、3步中的name要一致

        4:写init初始化方法,因为每个单例类初始化的内容不一样,所以没有写到宏里面,在创建单例类时记得写init方法

        5: 创建单例对象 [ 类名 share+(name)]

例如我们创建一个Person类的单例: 1 在Person的.h 文件中 singleton_h(Person)

                             2 在Person的.m 文件中 singleton_m(Person)

                             3 在Person的.m文件中写初始化方法

                             -(instancetype)init{

                                static id obj;

                                static dispatch_once_t onceToken;

                                dispatch_once(&onceToken, ^{

                                if ((obj = [super init])) {

                                //这里进行本类的初始化

                                    }

                                });

                                self = obj;

                                return self;

                             }

                             }

                             4 在使用的地方包含Person这个类的头文件 [ Person sharePerson ];

// ## :连接字符串和参数

// \ :表示下一行也是当前行的内容

 */

#ifndef Singleton_h

#define Singleton_h

 

#define singleton_h(name) +(instancetype)share##name;

#if __has_feature(objc_arc) // ARC

 

#define singleton_m(name) \

static id instance;\

+(instancetype)allocWithZone:(struct _NSZone *)zone{\

\

static dispatch_once_t onceToken;\

dispatch_once(&onceToken, ^{\

instance = [super allocWithZone:zone];\

});\

\

return instance;\

}\

\

+(instancetype)share##name{\

return [[self alloc]init];\

}\

+(id)copyWithZone:(struct _NSZone *)zone{\

return instance;\

}

 

#else //非ARC

 

#define singleton_m(name) \

static id instance;\

+(instancetype)allocWithZone:(struct _NSZone *)zone{\

    \

    static dispatch_once_t onceToken;\

    dispatch_once(&onceToken, ^{\

        instance = [super allocWithZone:zone];\

    });\

    \

    return instance;\

}\

\

+(instancetype)share##name{\

    return [[self alloc]init];\

}\

\

-(oneway void)release{\

    \

    \

}\

-(instancetype)autorelease{\

    return instance;\

}\

\

-(instancetype)retain{\

    return instance;\

}\

\

+(id)copyWithZone:(struct _NSZone *)zone{\

    return instance;\

}\

\

-(NSUInteger)retainCount{\

    return 1;\

}

 

#endif

#endif




原标题:iOS 开发中的单例

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

墨西哥人们眼里的圣诞节是怎样的?:https://www.kjdsnews.com/a/739937.html
服装卖家危!5大品牌接连起诉,有卖这些产品的立即下架!:https://www.kjdsnews.com/a/739938.html
留评47000+!亚马逊爆款宝宝学饮杯小心专利侵权!:https://www.kjdsnews.com/a/739939.html
Tiktok实现与Shopify互通,是资本的布局还是小卖家的机遇?:https://www.kjdsnews.com/a/739940.html
“难”字当头,今年入场的新卖家们过得如何?:https://www.kjdsnews.com/a/739941.html
韩国酷胖Coupang如何做到上架产品就出单?:https://www.kjdsnews.com/a/739942.html
千岛湖绿城度假酒店的简介:https://www.vstour.cn/a/363185.html
深圳大湾区游轮夜景 深圳大湾区游轮夜景图:https://www.vstour.cn/a/363186.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流