#import
@interface Session : NSObject
@property (strong,nonatomic) NSString *singleValue;
//实现单例方法
+ (Session *) GetInstance;
@end
#import "Session.h"
@implementation Session
// 单例对象
static Session *instance;
// 单例
+ (Session *) GetInstance {
@synchronized(self) {
if (instance == nil) {
instance = [[self alloc] init];
}
}
return instance;
}
-(id) init
{
if (self = [super init]) {
self.singleValue = [[NSString alloc] init];
}
return self;
}
@end
然后在需要使用单例的类import 这个单例类
Session *session = [Session GetInstance];
session.singleValue = @"好神奇阿!";
NSString *value = session.singleValue;