1. 基本概念
NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名、密码。
2. 创建方法
在ViewController.m中
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Heiti SC Light'; color: #008400}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s7 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s8 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s9 {font: 11.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s10 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s11 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//创建圆角按钮
UIButton* btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame=CGRectMake(100, 100, 80, 40);
[btn setTitle:@"写入文件" forState:UIControlStateNormal];
//添加事件函数
[btn addTarget:self action:@selector(pressWrite) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
UIButton* btnRead=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btnRead.frame=CGRectMake(100, 200, 80, 40);
[btnRead setTitle:@"读出文件" forState:UIControlStateNormal];
[btnRead addTarget:self action:@selector(pressRead) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnRead];
}
-(void)pressWrite
{
//定义一个用户默认数据对象
//不需要alloc创建,用户默认数据对象单例模式
//standardUserDefaults:获取全局唯一实例对象
NSUserDefaults* ud=[NSUserDefaults standardUserDefaults];
//存储字符串对象
//P1 要存储的对象id
//P2 对象的名字key
[ud setObject:@"Mike" forKey:@"Name"];
//存储数据对象
NSNumber* num=[NSNumber numberWithInt:100];
[ud setObject:num forKey:@"Num"];
//存储整型数据
[ud setInteger:123 forKey:@"Int"];
//存储BOOL类型
[ud setBool:YES forKey:@"Bool"];
//存储浮点
[ud setFloat:1.55 forKey:@"Float"];
//存储数组
NSArray* array=[NSArray arrayWithObjects:@"11",@"22",@"33",nil];
[ud setObject:array forKey:@"Array"];
}
-(void)pressRead
{
NSUserDefaults* ud=[NSUserDefaults standardUserDefaults];
id object = [ud objectForKey:@"Name"];
NSString* name=(NSString*)object;
NSLog(@"name = %@",name);
object= [ud objectForKey:@"Num"];
NSNumber* num=(NSNumber*)object;
NSLog(@"num = %@",num);
NSInteger iV= [ud integerForKey:@"Int"];
NSLog(@"int = %ld",iV);
BOOL bV=[ud boolForKey:@"Bool"];
NSLog(@"bV=@d",bV);
}
再次运行的时候,可以让程序直接读取而不存储数据,发现以前保存的数据仍然可以读取到界面上。
其实它存储在应用程序内置的一个plist文件里,这个可以根据路径看到。
比如说这个是你的程序沙盒位置
/UsersLibrary/Application Support/iPhoneSimulator/4.1/Applicati*****/29788E40-AF47-45A0-8E92-3AC0F501B7F4/,(这个是应用程序对应在mac上的位置)
这个下面有/Library/Prefereces,里面有个plist文件,存储的就是你的userDefaults想要删掉的话,用removeObjectForKey或者删掉沙盒,也就是你的应用程序然后重新安装。