单件模式实用的小例子 存取数据
@interface NSuser : NSObject
@property (nonatomic, strong) NSString *name;
+(NSuser *)instance;
//+(id)instance;
@end
#import "NSuser.h"
static NSuser *user = nil;
@implementation NSuser
+(NSuser *)instance
//+(id)instance
{
if (!user) {
user = [[NSuser alloc] init];
}
return user;
}
@end
#import <Foundation/Foundation.h>
#import "NSuser.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
// 单例模式简单实用
[[NSuser instance] setValue:@"xzk" forKey:@"name"];
NSLog(@"%@", [[NSuser instance] valueForKey:@"name"]);
//
[NSuser instance].name = @"sdsd";
NSLog(@"%@", [[NSuser instance] valueForKey:@"name"]);
}
return 0;
}
注:这里用的是KVC机制
[[NSuser instance] setValue:@"xzk" forKey:@"name"];
[[NSuser instance] valueForKey:@"name"];
不要和字典的
setValue: forKey:
objectForKey:
单例模式的简单实用
最新推荐文章于 2014-02-28 12:29:56 发布