单例究竟怎么传值?今天终于会了
.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
时间: 2024-10-21 05:42:48