单例 实现 一个变量为 整个项目都可以用
单例究竟怎么传值?今天终于会了
.h
@interface MySharedClass : NSObject
@property (nonatomic, copy) NSString *sharedString;
+ (MySharedClass *)sharedInstance;
@end
.m
@implementation MySharedClass
+ (MySharedClass *)sharedInstance
{
static MySharedClass *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[MySharedClass alloc] init];
});
return sharedInstance;
}
@end
简单应用
[MySharedClass sharedInstance].sharedString