NSUserDefaults 在我们编写代码中是最常用的一个永久保存数据的方法,也是最简单的。

使用NSUserDefault需要注意:

1.数据的本地化保存不是实时的,如果需要实时保存,调用synchronize方法。

2.保存的数据必须是“不可变的”。

3.保存数据数据使用 setObjectforKey(不同数据类型提供了不同的方法,例如保存Integer使用SetIntegerforKey等),同理读取使用ObjectforKey(IntegerforKey)。

- (void) btnClick : (UIButton*) sender {
NSInteger tag = sender.tag; NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
if(tag == 101) {
[def setInteger:31 forKey:@"age"];
[def setObject:@"harry" forKey:@"name"];
NSArray *array = [NSArray arrayWithObjects:@"tongji", @"cumt", @"wsyz", nil];
[def setObject:array forKey:@"edu"];
//强制写入文件
[def synchronize];
} else if (tag == 102) {
NSInteger age = [def integerForKey:@"age"];
NSString *name = [def objectForKey:@"name"];
NSArray *edu = [def objectForKey:@"edu"]; NSLog(@"name is %@, age is %ld, edu is %@", name, (long)age, edu);
}
}

  

NSUserDefault的使用的更多相关文章

  1. plist文件、NSUserDefault 对文件进行存储的类、json格式解析

    ========================== 文件操作 ========================== Δ一 .plist文件 .plist文件是一个属性字典数组的一个文件: .plis ...

  2. 【IOS】ios中NSUserDefault与android中的SharedPreference用法简单对比

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3405308.html 有Android开发经验的朋友对Shar ...

  3. iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)

    iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notific ...

  4. iOS开发——数据持久化Swift篇&(一)NSUserDefault

    NSUserDefault //******************** 5.1 NSUserDefault和对象归档 func useNSUserDefault() { //通过单利来创建一个NSU ...

  5. NSUserDefault 的使用(好东东,留着)

    1.NSUserDefault的使用: 作用:NSUserDefaults类提供了一个与默认系统进行交互的编程接口.NSUserDefaults对象是用来保存,恢复应用程序相关的偏好设置,配置数据等等 ...

  6. 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错

    原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html iOS页面间传值的方式(NSUserDefault/Delegate/NSNotificatio ...

  7. 读写应用程序数据-NSUserDefault、对象归档(NSKeyedArchiver)、文件操作

    ios中数据持久化存储方式一般有5种:NSUserDefault.对象归档(NSKeyedArchiver).文件操作.数据库存储(SQLite3).CoreData. 1.NSUserDefault ...

  8. 不建议使用NSUserDefault存储大量数据

    NSUserDefaults 其实是一个 plist 文件(有待验证),即使只是修改一个 key 都会 load 整个文件,不适合存储大量数据. NSUserDefaults是保存成文本格式的,容易被 ...

  9. iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)

    iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)   iOS页面间传值的方式(NSUserDefault/Delegate/NSN ...

随机推荐

  1. html5 请求的URL转成 OC可用属性字符串显示

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:helpUrlStr]]; NSString *string = [ ...

  2. DataGridview 自动切换到 下一行

    if (m_dgvList.SelectedRows.Count > 0) { int intCurrApp = m_dgvList.SelectedRows[0].Index; if (int ...

  3. Linux手动释放缓存的方法

    Linux释放内存的命令:syncecho 1 > /proc/sys/vm/drop_caches drop_caches的值可以是0-3之间的数字,代表不同的含义:0:不释放(系统默认值)1 ...

  4. win32记事本程序(二)

    遇到一个较大的难题,做记事本要不要使用edit或者是richedit控件呢.如果用控件的话感觉没什么挑战,不用控件,现有的参考资料仅有<windows程序设计>第六章的TYPER程序,这个 ...

  5. mysql group by后 拼接某一字段

    SQL> select * from tmp10; JS    ND JM--------- ----- -------------------------------------------- ...

  6. the django travel(two)

    一:django路由系统: 注意:我们在urls.py中 定义url的时候,可以加$和不加$,区别的是:加$正则匹配的时候,比如:'/index/$'只能匹配'/index/'这样的url 不能匹配' ...

  7. Spring配置文件详解 - applicationContext.xml文件路径

    spring的配置文件applicationContext.xml的默认地址在WEB-INF下,只要在web.xml中加入代码 org.springframework.web.context.Cont ...

  8. CSS hack方式一览【转】

    做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我们会极不情愿的使用这个不太友好的方式来达到大家要求的页面表现.我个人是不太推荐使用hack的,要知道 ...

  9. div隐藏与显示

    <input type="button" value="隐藏详情" class="jishu_n_k1_input2" id=&quo ...

  10. android之animation

    Android  Animation一共有四种 Alpha: 淡入淡出效果 Scale: 缩放效果 Rotate: 旋转效果 Translate:移动效果 使用Tweened Animations的步 ...