談一談iOS單例模式
單例模式是一種常用的軟件設(shè)計(jì)模式。在它的核心結(jié)構(gòu)中只包含一個(gè)被稱為單例的特殊類。通過(guò)單例模式可以保證系統(tǒng)中一個(gè)類只有一個(gè)實(shí)例而且該實(shí)例易于外界訪問(wèn),從而方便對(duì)實(shí)例個(gè)數(shù)的控制并節(jié)約系統(tǒng)資源。如果希望在系統(tǒng)中某個(gè)類的對(duì)象只能存在一個(gè),單例模式是最好的解決方案。
1、書(shū)寫(xiě)步驟
1)、創(chuàng)建類方法,返回對(duì)象實(shí)例.以shared default current開(kāi)頭。
2)、創(chuàng)建一個(gè)全局變量用來(lái)保存對(duì)象的引用
3)、判斷對(duì)象是否存在,若不存在,創(chuàng)建對(duì)象
2、具體單例模式的幾種模式
第一種單例模式
//非線程安全寫(xiě)法
static UserHelper * helper = nil;
+ (UserHelper *)sharedUserHelper {
if (helper == nil) {
helper = [[UserHelper alloc] init];
}
return helper;
}
第二種單例模式
//線程安全寫(xiě)法1
static UserHelper * helper = nil;
+ (UserHelper *)sharedUserHelper {
@synchronized(self) {
if (helper == nil) {
helper = [[UserHelper alloc] init];
}
}
return helper;
}
第三種單例模式
+ (void)initialize {
if ([self class] == [UserHelper class]) {
helper = [[UserHelper alloc] init];
}
}
第四種單例模式
//線程安全寫(xiě)法3(蘋果推薦,主要用這個(gè))
static UserHelper * helper = nil;
+ (UserHelper *)sharedUserHelper {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
helper = [[UserHelper alloc] init];
});
return helper;
}
MRC全面實(shí)現(xiàn)單例寫(xiě)法(了解)
#import <Foundation/Foundation.h>
#import "UserHelper.h"
void func() {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"haha");
});
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// [UserHelper logout];
if ([UserHelper isLogin]) {
UserHelper * helper = [UserHelper sharedUserHelper];
NSLog(@"username = %@ password = %@",helper.userName,helper.password);
} else {
char name[20];
char pwd[20];
NSLog(@"請(qǐng)輸入用戶名");
scanf("%s",name);
NSLog(@"請(qǐng)輸入密碼");
scanf("%s",pwd);
NSString * userName = [[NSString alloc] initWithUTF8String:name];
NSString * password = [[NSString alloc] initWithUTF8String:pwd];
if (userName && password) {
[UserHelper loginWithUserName:userName password:password];
UserHelper * helper = [UserHelper sharedUserHelper];
NSLog(@"username = %@ password = %@",helper.userName,helper.password);
}
}
// UserHelper * help1 = [UserHelper sharedUserHelper];
// help1.userName = @"dahuan";
// help1.password = @"123456";
// NSLog(@"%p",help1);
// NSLog(@"%@",help1.userName);
// NSLog(@"%@",help1.password);
//
//
// UserHelper * help2 = [UserHelper sharedUserHelper];
// help2.password = @"zxc";
// NSLog(@"%p",help2);
// NSLog(@"%@",help1.userName);
// NSLog(@"%@",help1.password);
}
return 0;
}
//class.h
#import <Foundation/Foundation.h>
@interface UserHelper : NSObject
//1、創(chuàng)建類方法,返回對(duì)象實(shí)例 shared default current
+ (UserHelper *)sharedUserHelper;
@property (nonatomic, copy) NSString * userName;
@property (nonatomic, copy) NSString * password;
+ (BOOL)isLogin;
+ (void)loginWithUserName:(NSString *)userName password:(NSString *)password;
+ (void)logout;
@end
// class.m
#import "UserHelper.h"
//2、創(chuàng)建一個(gè)全局變量
#define Path @"/Users/dahuan/Desktop/data"
static UserHelper * helper = nil;
@implementation UserHelper
//+ (void)initialize {
//
// if ([self class] == [UserHelper class]) {
// helper = [[UserHelper alloc] init];
// }
//}
+ (UserHelper *)sharedUserHelper {
//3、判斷對(duì)象是否存在,若不存在,創(chuàng)建對(duì)象
//線程安全
// @synchronized(self) {
//
// if (helper == nil) {
// helper = [[UserHelper alloc] init];
// }
// }
//gcd 線程安全
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
helper = [[UserHelper alloc] init];
});
return helper;
}
- (instancetype)init {
if (self = [super init]) {
NSString * data = [NSString stringWithContentsOfFile:Path encoding:NSUTF8StringEncoding error:nil];
if (data) {
NSArray * array = [data componentsSeparatedByString:@"-"];
_userName = array[0];
_password = array[1];
}
}
return self;
}
+ (BOOL)isLogin {
UserHelper * helper = [UserHelper sharedUserHelper];
if (helper.userName && helper.password) {
return YES;
}
return NO;
}
+ (void)loginWithUserName:(NSString *)userName password:(NSString *)password {
UserHelper * helper = [UserHelper sharedUserHelper];
helper.userName = userName;
helper.password = password;
NSArray * array = @[userName,password];
NSString * data = [array componentsJoinedByString:@"-"];
[data writeToFile:Path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
+ (void)logout {
NSFileManager * fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:Path]) {
[fm removeItemAtPath:Path error:nil];
}
}
@end
以上就是關(guān)于iOS單例模式的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
簡(jiǎn)單掌握iOS應(yīng)用開(kāi)發(fā)中sandbox沙盒的使用
這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中sandbox沙盒的使用,即將應(yīng)用的存儲(chǔ)區(qū)域單獨(dú)隔離開(kāi)來(lái),開(kāi)發(fā)時(shí)經(jīng)??梢杂玫?需要的朋友可以參考下2016-01-01
iOS開(kāi)發(fā)教程之常見(jiàn)的性能優(yōu)化技巧
這篇文章主要給大家介紹了關(guān)于iOS開(kāi)發(fā)教程之常見(jiàn)的性能優(yōu)化技巧,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用iOS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
iOS布局渲染之UIView方法的調(diào)用時(shí)機(jī)詳解
在你剛開(kāi)始開(kāi)發(fā) iOS 應(yīng)用時(shí),最難避免或者是調(diào)試的就是和布局相關(guān)的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于iOS布局渲染之UIView方法調(diào)用時(shí)機(jī)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07
iOS UICollectionView實(shí)現(xiàn)卡片效果
這篇文章主要為大家詳細(xì)介紹了iOS UICollectionView實(shí)現(xiàn)卡片效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
iOS中UITextField實(shí)現(xiàn)過(guò)濾選中狀態(tài)拼音的代碼
這篇文章主要介紹了iOS中UITextField實(shí)現(xiàn)過(guò)濾選中狀態(tài)拼音的代碼,需要的朋友可以參考下2018-01-01
解決ios端點(diǎn)擊按鈕閃爍問(wèn)題(小tips)
這篇文章主要介紹了ios端點(diǎn)擊按鈕閃爍的解決方法(小tips),需要的朋友參考下吧2017-10-10
總結(jié)iOS實(shí)現(xiàn)漸變顏色的三種方法
這篇文章主要給大家總結(jié)了iOS實(shí)現(xiàn)漸變顏色的三種方法,分別是利用CAGradientLayer實(shí)現(xiàn)漸變、Core Graphics相關(guān)方法實(shí)現(xiàn)漸變以及用CAShapeLayer作為layer的mask屬性實(shí)現(xiàn),大家可以根據(jù)自己的需要選擇使用,下面來(lái)一起看看吧。2016-10-10
iOS UIScrollView和控制器返回手勢(shì)沖突解決方法
這篇文章主要介紹了iOS UIScrollView和控制器返回手勢(shì)沖突解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
iOS 導(dǎo)航欄無(wú)縫圓滑的隱藏 Navigationbar實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹了iOS 導(dǎo)航欄無(wú)縫圓滑的隱藏 Navigationbar的效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11

