ios 单例宏

作者: oc123 | 来源:发表于2017-05-12 19:51 被阅读46次

单例的初始化在整个app生命周期内(非对象的生命周期)只执行一次,本文介绍通过宏来实现单例方法的定义。代码如下:
----- 单例宏定义 - 头文件 -----
类名 - Singleton.h

// .h文件
#define HMSingletonH(name) + (instancetype)shared##name;

// .m文件
#if __has_feature(objc_arc)

    #define HMSingletonM(name) \
    static id _instace; \
 \
    + (id)allocWithZone:(struct _NSZone *)zone \
    { \
        if (_instace == nil) {\
            _instace = [super allocWithZone:zone]; \
        } \
        return _instace; \
    } \
 \
    + (instancetype)shared##name \
    { \
        if (_instace == nil) {\
            _instace = [[self alloc]init]; \
        } \
      return _instace; \
  \
    } \
 \
    - (id)copyWithZone:(NSZone *)zone \
    { \
        return _instace; \
    }

#else

    #define HMSingletonM(name) \
    static id _instace; \
 \
    + (id)allocWithZone:(struct _NSZone *)zone \
    { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
            _instace = [super allocWithZone:zone]; \
        }); \
        return _instace; \
    } \
 \
    + (instancetype)shared##name \
    { \
        static dispatch_once_t onceToken; \
        dispatch_once(&onceToken, ^{ \
            _instace = [[self alloc] init]; \
        }); \
        return _instace; \
    } \
 \
    - (id)copyWithZone:(NSZone *)zone \
    { \
        return _instace; \
    } \
 \
    - (oneway void)release { } \
    - (id)retain { return self; } \
    - (NSUInteger)retainCount { return 1;} \
    - (id)autorelease { return self;}

#endif

----- 需要参加单例的类 -----
类名 - SingletonClass.h

#import <Foundation/Foundation.h>
#import "Singleton.h"

@interface SingletonClass : NSObject
HMSingletonH(SingletonClass)//单例模式
@end

类名 - SingletonClass.m

#import "SingletonClass.h"

@implementation SingletonClass
HMSingletonM(SingletonClass)//单例模式
- (instancetype)init {
    if (self = [super init]) {
        NSLog(@"单例创建!");
    }
    return self;
}
@end

----- 创建单例 -----
类名 - ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

类名 - ViewController.m

#import "ViewController.h"
#import "SingletonClass.h"

@interface ViewController ()

@end

static int i;//计数
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    i = 0;
}
/**
 *  xib - button action linked
 */
- (IBAction)buttonClick:(UIButton *)sender {
    NSLog(@"点击次数:%d",++i);
    [SingletonClass sharedSingletonClass];
}
@end

点击button后,打印截图如下:

单例效果截图.png

另附,dispatch_once可用于创建单例,但dispatch_once整个app生命周期内只执行一次,不是对象生命周期内只执行一次;

相关文章

  • ios 单例宏

    单例的初始化在整个app生命周期内(非对象的生命周期)只执行一次,本文介绍通过宏来实现单例方法的定义。代码如下:-...

  • iOS单例宏

    开发中我们无可避免的使用到单例,单例的具体作用不多说了,这里记录一下单例宏的写法。新建一个header file,...

  • iOS 单例宏

    #if __has_feature(objc_arc) #define SYNTHESIZE_SINGLETON_...

  • 单例

    单例 单例宏

  • 单例

    iOS单例模式iOS之单例模式初探iOS单例详解

  • iOS 常用单例宏

    在你的宏文件里面加入单例宏,快捷创建单例 调用 .h .m

  • OC - iOS - 宏单例

    Macro单例

  • iOS单例--宏定义

    如下是单例,是一个宏。这样只有工程中用到单例直接用此宏创建,大大节约时间。 如下是通过宏文件如何创建单例。在.h中...

  • IOS OC 单例宏

  • 单例模式和GCD单例实现

    1、传统单例模式2、GCD单例模式3、用宏实现GCD单例模式4、用宏实现GCD单例模式,名称随类名变化而变化 单例...

网友评论

    本文标题:ios 单例宏

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