一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹。靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希望未来技术之巅上有你们也有我。
这个单例主要是用在项目中的Model,使用在Model中之后,整个Model可以在项目中的任何一个地方都能够获取到model中的值.开发项目中比较重要,也很常用,例如获取手机号码,用户ID,状态信息等等…可以存储在这个模型里面
使用
.h文件
@interface BaseModel : NSObject 写在里面
//做单例的目的是为了 全局获取登录模型中的数据
Single_interface(BaseModel)
.m文件
@implementation BaseModel 写在里面
Single_implementation(BaseModel)
A控制器
#import "ViewController.h"
#import "FHXViewController.h"
#import "BaseModel.h"
@interface ViewController ()
@property (nonatomic,strong) BaseModel *model;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.model = [BaseModel new];
self.model.name = @"冯汉栩";
self.model.age = @"16";
[self.navigationController pushViewController:[FHXViewController new] animated:YES];
}
@end
B控制器
#import "FHXViewController.h"
#import "BaseModel.h"
@interface FHXViewController ()
@property (nonatomic,strong) BaseModel *model;
@end
@implementation FHXViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.model = [BaseModel new];
NSLog(@"name - %@",self.model.name);
NSLog(@"age - %@",self.model.age);
}
@end
打印结果: