NSUserDefaults — 写读&配置

  • Post author:
  • Post category:其他


<pre name="code" class="objc">NSUserDefaults 
1.可以保存一些简单的数据
2.可以定制应用的行为


 NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
//write
    [d setObject:@"iCrazy" forKey:@"name"];
    [d setInteger:20 forKey:@"age"];
    [d setFloat:1.81 forKey:@"height"];
    [d synchronize];//将内存里的数据长久保存
    
//read
    NSString *name = [d objectForKey:@"name"];
    NSInteger *age = [d integerForKey:@"age"];
    float height = [d floatForKey:@"height"];
//print
    NSLog(@"name = %@,age = %d,height = %.2f",name,age,height);

setting bundle  里添加以开关,其他为自动添加的控件




获取setting中的信息:

  NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    bool wifi_switch = [d boolForKey:@"WIFI"];
    NSString *name = [d objectForKey:@"name_preference"];
    NSLog(@"wifi switch = %d,name = %@",wifi_switch,name);

//   配置完设置,如果setting里没有显示,就重新启动iphone模拟器即可,我刚开始也是试了好多遍才知道重启。



版权声明:本文为weidaoRan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。