單例形式是一種經常使用的軟件設計形式。在它的焦點構造中只包括一個被稱為單例的特別類。經由過程單例形式可以包管體系中一個類只要一個實例並且該實例易於外界拜訪,從而便利對實例個數的掌握並勤儉體系資本。假如願望在體系中某個類的對象只能存在一個,單例形式是最好的處理計劃。
1、書寫步調
1)、創立類辦法,前往對象實例.以shared default current開首。
2)、創立一個全局變量用來保留對象的援用
3)、斷定對象能否存在,若不存在,創立對象
2、詳細單例形式的幾種形式
第一種單例形式
//非線程平安寫法
static UserHelper * helper = nil;
+ (UserHelper *)sharedUserHelper {
if (helper == nil) {
helper = [[UserHelper alloc] init];
}
return helper;
}
第二種單例形式
//線程平安寫法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];
}
}
第四種單例形式
//線程平安寫法3(蘋果推舉,重要用這個)
static UserHelper * helper = nil;
+ (UserHelper *)sharedUserHelper {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
helper = [[UserHelper alloc] init];
});
return helper;
}
MRC周全完成單例寫法(懂得)
#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(@"請輸出用戶名");
scanf("%s",name);
NSLog(@"請輸出暗碼");
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、創立類辦法,前往對象實例 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、創立一個全局變量
#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、斷定對象能否存在,若不存在,創立對象
//線程平安
// @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
以上就是關於IOS單例形式的全體內容,願望對年夜家的進修有所贊助。
【談一談iOS單例形式】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!