iOS开发

Object-C单例写法2018

2018-09-30  本文已影响40人  勇往直前888

在项目中,单例用得比较多,当然代码基本上网上都有。大多数情况,也基本上自己写自己用,所以不会出现单例还用copy,alloc init之类的问题。
这里有一篇文章,感觉很不错,照着写了一下,基本上可以,当然稍有不同,所以备忘一下。

Objective-c单例模式的正确写法

调用接口

+ (instancetype)sharedInstance;
+ (instancetype)sharedInstance {
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[super allocWithZone:NULL] init];
    });
    return instance;
}

重写方法

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [[self class] sharedInstance];
}
+ (id)copyWithZone:(struct _NSZone *)zone OBJC_ARC_UNAVAILABLE;
+ (id)mutableCopyWithZone:(struct _NSZone *)zone OBJC_ARC_UNAVAILABLE;
- (id)copy {
    return [[self class] sharedInstance];
}

- (id)mutableCopy {
    return [[self class] sharedInstance];
}

例子代码

这样一来,所有的单例就可以用完全相同的代码来表示了:

+ (instancetype)sharedInstance {
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[super allocWithZone:NULL] init];
    });
    return instance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [[self class] sharedInstance];
}

- (id)copy {
    return [[self class] sharedInstance];
}

- (id)mutableCopy {
    return [[self class] sharedInstance];
}

测试代码

    KJTWebViewManager* obj1 = [KJTWebViewManager sharedInstance] ;
    NSLog(@"obj1 = %@.", obj1);
    KJTWebViewManager* obj2 = [KJTWebViewManager sharedInstance] ;
    NSLog(@"obj2 = %@.", obj2);
    KJTWebViewManager* obj3 = [[KJTWebViewManager alloc] init];
    NSLog(@"obj3 = %@.", obj3);
    KJTWebViewManager* obj4 = [[KJTWebViewManager alloc] init];
    NSLog(@"obj4 = %@.", [obj4 copy]);
    KJTWebViewManager* obj5 = [[KJTWebViewManager alloc] init];
    NSLog(@"obj4 = %@.", [obj5 mutableCopy]);
2018-09-30 13:38:12.659190+0800 Wallet[42247:11822152] +[KJTWebView showWithController:url:] 第18行 
 obj1 = <KJTWebViewManager: 0x600001ab1630>.


2018-09-30 13:38:12.659417+0800 Wallet[42247:11822152] +[KJTWebView showWithController:url:] 第20行 
 obj2 = <KJTWebViewManager: 0x600001ab1630>.


2018-09-30 13:38:12.659550+0800 Wallet[42247:11822152] +[KJTWebView showWithController:url:] 第22行 
 obj3 = <KJTWebViewManager: 0x600001ab1630>.


2018-09-30 13:38:12.659635+0800 Wallet[42247:11822152] +[KJTWebView showWithController:url:] 第24行 
 obj4 = <KJTWebViewManager: 0x600001ab1630>.


2018-09-30 13:38:14.272292+0800 Wallet[42247:11822152] +[KJTWebView showWithController:url:] 第26行 
 obj5 = <KJTWebViewManager: 0x600001ab1630>.

宏替换

既然所有的单例都完全一样的代码,那么就考虑用宏来进行统一的替换。

#define kSingletonInterface    + (instancetype)sharedInstance;
#define kSingletonImplementation \
\
+ (instancetype)sharedInstance { \
    static id instance = nil; \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        instance = [[super allocWithZone:NULL] init]; \
    }); \
    return instance; \
} \
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone { \
    return [[self class] sharedInstance]; \
} \
\
- (id)copy { \
    return [[self class] sharedInstance]; \
} \
\
- (id)mutableCopy { \
    return [[self class] sharedInstance]; \
}

使用宏

@interface KJTWebViewManager : NSObject

kSingletonInterface

@end
#import "KJTWebViewManager.h"

@implementation KJTWebViewManager

kSingletonImplementation

@end

参考文章

下面两篇文章都不错,值得认真看看,推荐

上一篇 下一篇

猜你喜欢

热点阅读