iOS 宏~单例封装宏

//联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄

/**

1.SH前缀是作者名称简称"SH".

2.宏定义直接拷贝到pch文件中即可.

*/

一、单例使用

1.使用方法.h

#import

@interfaceMineAuthentication :NSObject

SHSingleInstance_H_(AuthenticationManager )

@end

2.使用方法.m

#import"SHMineAuthentication.h"

@interfaceMineAuthentication ()

@end

@implementationMineAuthentication

SHSingleInstance_M_(AuthenticationManager )

@end

二、单例实现和定义

//1.单例实现和定义宏--方便.h文件使用

#define SHSingleInstance_H_(name) \

+ (instancetype)shared##name;

//2.单例实现和定义宏--方便.m文件使用

#define SHSingleInstance_M_(name) \

static id _instance = nil; \

+ (instancetype)shared##name \

{ \

static dispatch_once_t onceToken; \

dispatch_once(&onceToken, ^{ \

if (_instance == nil) \

{ \

_instance = [[self alloc] init]; \

} \

}); \

return _instance; \

} \

\

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

{ \

static dispatch_once_t onceToken; \

dispatch_once(&onceToken, ^{ \

if (_instance == nil) \

{ \

_instance = [super allocWithZone:zone]; \

} \

}); \

return _instance; \

} \

\

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

{ \

return _instance; \

} \

+ (id)mutableCopyWithZone:(struct _NSZone *)zone \

{ \

return _instance; \

}

三、单例调用

[MineAuthenticationsharedAuthenticationManager];

谢谢!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在开发中经常会用到单例设计模式,目的就是为了在程序的整个生命周期内,只会创建一个类的实例对象,而且只要程序不被杀死...
    不要重名就好阅读 557评论 0 0
  • 在开发中经常会用到单例设计模式,目的就是为了在程序的整个生命周期内,只会创建一个类的实例对象,而且只要程序不被杀死...
    VincentHK阅读 685评论 0 3
  • 在开发中经常会用到单例设计模式,目的就是为了在程序的整个生命周期内,只会创建一个类的实例对象,而且只要程序不被杀死...
    零度_不结冰阅读 447评论 0 0
  • 20151001,读完冶文彪《清明上河图密码》 20151002,读完《清明上河图密码2》 此两部小说描绘宋时市井...
    周石阅读 301评论 0 2
  • 你还在为孩子一直沉迷手机游戏而担忧吗?你还在为孩子如何开发大脑潜能而不知所措吗?来,来,来,来参加霞涌村魔...
    徒步独行阅读 767评论 0 1