日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS单例初步理解

發(fā)布時間:2023/12/13 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS单例初步理解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

iOS單例初步理解

在iOS開發(fā)中,系統(tǒng)自帶的框架中使用了很多單例,非常方便用戶(開發(fā)者,使用比如[NSApplication sharedApplication] 等),在實際的開發(fā)中,有時候也需要設(shè)計單例對象,為保證每次獲取的對象都為同一個對象。
在iOS開發(fā)中創(chuàng)建單例具體步驟:
1.提供一個類方法:+ (instancetype)sharedXXXX;
2.創(chuàng)建一個全局靜態(tài)變量static id _instance;
3.重寫allocWithZone
4.重寫copyWithZone

特舉例子如下:
@interface MusicTool : NSObject
+ (instancetype)sharedMusicTool;
@end
static id _instance; // 全局變量
/**
* alloc方法內(nèi)部會調(diào)用allocWithZone
*/
+ (id)allocWithZone:(struct _NSZone *)zone {
if (_instance == nil) {
@synchronized(self) {
if (_instance == nil) {
_instance = [super allocWithZone:zone];
}
}
}
return _instance;
}
/**
* 重寫copy方法,防止copy出錯
*/
- (instancetype)copyWithZone:(NSZone *)zone {
return _instance;
}

  • (instancetype)sharedMusicTool {
    if (_instance == nil) {
    @synchronized(self) {
    if (_instance == nil) {
    _instance = [[self alloc] init];
    }
    }
    }
    return _instance;
    }

第二種使用GCD創(chuàng)建單例方法
@interface DataTool : NSObject
+ (instancetype)shareDataTool;
@end
static id _instance;
+ (id)allocWithZone:(struct _NSZone *)zone {
if (_instance == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [super allocWithZone:zone];
});
}
return _instance;
}
- (instancetype)copyWithZone:(NSZone *)zone {
return _instance;
}

  • (instancetype)shareDataTool {
    if (_instance == nil) {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    _instance = [[self alloc]init];
    });
    }
    return _instance;
    }
    第三種使用餓漢模式
    @interface SoundTool : NSObject
  • (instancetype)sharedSoundTool;
    @end

static id _instance;
+ (void)load {
_instance = [[self alloc]init];
}

  • (instancetype)allocWithZone:(struct _NSZone *)zone {
    if (_instance == nil) {
    _instance = [super allocWithZone:zone];
    }
    return _instance;
    }

  • (instancetype)sharedSoundTool {
    return _instance;
    }

  • (instancetype)copyWithZone:(NSZone *)zone {
    return _instance;
    }

為保證兼容MRC還需要重寫
static id _instace;

  • (id)allocWithZone:(struct _NSZone *)zone
    {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    _instace = [super allocWithZone:zone];
    });
    return _instace;
    }

  • (instancetype)sharedDataTool
    {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    _instace = [[self alloc] init];
    });
    return _instace;
    }

  • (id)copyWithZone:(NSZone *)zone
    {
    return _instace;
    }

  • (oneway void)release { } //重寫release

  • (id)retain { return self; } //重寫retain
  • (NSUInteger)retainCount { return 1;} //重寫retainCount
  • (id)autorelease { return self;} //重寫autorelease

總結(jié)

以上是生活随笔為你收集整理的iOS单例初步理解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。