//非线程安全写法
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;
}
#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
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有