1、load方法:
1> 当类被引用进程序的时候会执行这个函数
2> 一个类的load方法不用写明[super load],父类就会收到调用,并且在子类之前。
3> Category的load也会收到调用,但顺序上在主类的load调用之后。
2、initialize方法:
1> initialize的自然调用是在第一次主动使用当前类的时候
2> 和load不同,即使子类不实现initialize方法,会把父类的实现继承过来调用一遍。注意的是在此之前,父类的方法已经被执行过一次了,同样不需要super调用。
3、load和initialize有很多共同特点,下面简单列一下:
1> 在不考虑开发者主动使用的情况下,系统最多会调用一次
2> 如果父类和子类都被调用,父类的调用一定在子类之前
3> 都是为了应用运行提前创建合适的运行环境
4> 在使用时都不要过重地依赖于这两个方法,除非真正必要
由于initialize的这些特点,使得其应用比load要略微广泛一些。可用来做一些初始化工作,或者单例模式的一种实现方案
4、单例模式
1> 概念
单例模式是一种常用的软件设计模式。在它的核心结构中只包含一个被称为单例的特殊类。通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。
2> 书写步骤
(1)创建类方法,返回对象实例.以shared default current开头。
(2)创建一个全局变量用来保存对象的引用
(3)判断对象是否存在,若不存在,创建对象
3> 非线程安全写法
static UserHelper * helper = nil;
+ (UserHelper *)sharedUserHelper {
if (helper == nil) {
helper = [[UserHelper alloc] init];
}
return helper;
}
4> 线程安全写法1
static UserHelper * helper = nil;
+ (UserHelper *)sharedUserHelper {
@synchronized(self) {
if (helper == nil) {
helper = [[UserHelper alloc] init];
}
}
return helper;
}
5> 线程安全写法2
+ (void)initialize {
if ([self class] == [UserHelper class]) {
helper = [[UserHelper alloc] init];
}
}
6、线程安全写法3(苹果推荐,主要用这个)
static UserHelper * helper = nil;
+ (UserHelper *)sharedUserHelper {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
helper = [[UserHelper alloc] init];
});
return helper;
}
7、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 |