1。
NSString *path = [[NSBundle mainBundle] pathForResource:@"文件名" ofType:@"plist"];
// 文件数据类型是array
NSArray *array=[NSArray arrayWithContentsOfFile:path];
//文件数据类型是*dictionary
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
 
 
//1. 创建一个plist文件
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
    NSString *path=[paths    objectAtIndex:0];
    NSLog(@"path = %@",path);
    NSString *filename=[path stringByAppendingPathComponent:@"test.plist"];    
    NSFileManager* fm = [NSFileManager defaultManager];
    [fm createFileAtPath:filename contents:nil attributes:nil];        
    //NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    
    //创建一个dic,写到plist文件里
    NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:@"sina",@"1",@"163",@"2",nil];
    [dic writeToFile:filename atomically:YES];
    
    //读文件
    NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename];
    NSLog(@"dic is:%@",dic2);
 
 

2。NSUserDefaults的使用

创建一个user defaults方法有多个,最简单得快速创建方法:
   NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];

添加数据到 user defaults:
   [accountDefaults setObject:nameField.text forKey:UserDefaultNameKey];
也可以添加基本数据类型int, float, bool等,有相应得方法

[accountDefaults setBool:YES forKey:UserDefaultBoolKey];

从user defaults中获取数据:

[accountDefaults objectForKey:NCUserDefaultNameKey]

[accountDefaults boolForKey: UserDefaultBoolKey];

要点: NSUserDefaults非常好用,并不需要用户在程序中设置NSUserDefaults的全局变量,需要在哪里使用NSUserDefaults的数据,那么就在哪里创建一个NSUserDefaults对象,然后进行读或者写操作。
     针对同一个关键字对应的对象或者数据,可以对它进行重写,重写之后关键字就对应新的对象或者数据,旧的对象或者数据会被自动清理。

 
3 关于NSUserDefaults保存不了数据的问题If you terminate your app by pressing the home button (in the Simulator or on the device), your User Defaults will get saved.

如果你按HOME键终止你的应用(真机或者模拟器上),你的值是会被保存的。

If you terminate your app by pressing "Stop" in Xcode (in the Simulator or on the device), your User Defaults might get saved, but there's a good chance they won't. NSUserDefaults persists any changes periodically, and if you terminate the process before they've been persisted, they'll be gone. You can force the save by calling:

[[NSUserDefaults standardUserDefaults] synchronize];
 
如果你在XCODE中,终止你的应用(在模拟器或者真机上),你的值有时候(terminate the process before they've been persisted(不知道是个什么鬼))不会被保存。但是你可以使用以下代码强制保存:
[[NSUserDefaults standardUserDefaults] synchronize];

ios 保存本地数据的方法的更多相关文章

  1. iOS - LocalCache 本地数据缓存

    1.自定义方式本地数据缓存 1.1 自定义缓存 1 沙盒路径下的 Library/Caches 用来存放缓存文件,保存从网络下载的请求数据,后续仍然需要继续使用的文件,例如网络下载的离线数据,图片,视 ...

  2. [转]bootstrap table本地数据使用方法

    原文地址:http://www.giserdqy.com/language/h5/bootstrap/1136 data对应columns进行组合 var columns = [ { field: ' ...

  3. UserDefault的使用,保存小数据到本地-iOS

    //保持到本地数据 NSArray *array=@[@"234",@"sdfe"]; NSUserDefaults *userDefault=[NSUserD ...

  4. iOS APP之本地数据存储(译)

    最近工作中完成了项目的用户信息本地存储,查阅了一些本地存储加密方法等相关资料.期间发现了一个来自印度理工学院(IIT)的信息安全工程师的个人博客,写了大量有关iOS Application secur ...

  5. iOS开发技术分享(1)— iOS本地数据存储

    iOS开发技术分享(1)— iOS本地数据存储 前言: 我本是一名asp.net程序员,后来加入了iOS游戏开发队伍,到现在也有一年多的时间了.这一年来,每天都干到2.3点钟才睡觉,不为别的,只为了学 ...

  6. IOS开发中数据持久化的几种方法--NSUserDefaults

    IOS开发中数据持久化的几种方法--NSUserDefaults IOS 开发中,经常会遇到需要把一些数据保存在本地的情况,那么这个时候我们有以下几种可以选择的方案: 一.使用NSUserDefaul ...

  7. 最全的iOS数据存储方法

    目的 项目准备运用的Core Data进行本地数据存储,本来打算只写一下Core Data的,不过既然说到了数据存储,干脆来个数据存储基础大总结!本文将对以下几个模块进行叙述. 沙盒 Plist Pr ...

  8. [ios]ios读写文件本地数据

    参考:http://blog.csdn.net/tianyitianyi1/article/details/7713103 ios - Write写入方式:永久保存在磁盘中.具体方法为:第一步:获得文 ...

  9. iOS保存数据的4种方式

    在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好.下面介绍一下数据保存的方式: 1.NSKeyed ...

随机推荐

  1. jQuery点击图片弹出大图遮罩层

    使用jQuery插件HoverTreeShow弹出遮罩层显示大图 效果体验:http://hovertree.com/texiao/hovertreeshow/ 在开发HoverTreeTop项目的产 ...

  2. Nginx - Rewrite Module

    Initially, the purpose of this module (as the name suggests) is to perform URL rewriting. This mecha ...

  3. 自动恢复被挂掉的hbase region server

    最近老遇到hbase的regionserver自动挂掉的情况 日志总是看不出什么原因 不得已弄了个定时任务,任务的脚本类似是: pssh -H "bluejoe1 bluejoe2...&q ...

  4. DialogFragment

    DialogFragment 从Android 3.0 (API level 11)开始引入,如果想在低于该版本的系统上使用,需用android.support.v4.app.DialogFragme ...

  5. 并行执行的Service,以媒体转码成新格式为例

    大家众所周知,IntentService内置的handler只有一个线程,而AsyncTask又只适合时间至多几秒的操作,所以我们关注使用ExecutorService建立并行执行.为了确保Servi ...

  6. linux umount 提示device is busy 的解决

    linux umount 提示"device is busy" 终极解决 为了干净地关闭或热交换 UNIX 或类 UNIX 系统上的存储硬件,必须能够卸载使用此设备上的存储的所有文件系统.但是,如果正 ...

  7. BT之下拉菜单

    <div class="dropdown"> <button class="btn btn-default dropdown-toggle" ...

  8. iptables开始ftp

    如果本机做FTP被访问 iptables -I INPUT -p tcp --dport 21 -m state --state NEW -j ACCEPT //开放21端口iptables -I I ...

  9. 文档生产工具 Doxygen

    Doxygen是一种开源跨平台的,类似JavaDoc风格描述的文档系统,支持C.C++.Java.Objective-C等语言.可以从一套归档源文件开始,生成HTML,XML,pdf等不同风格的格式. ...

  10. Java调用外部程序常用算法和封装类

    一个项目不可能只使用一种编程语言来开发,也不可能由一个人开发,所以,Java程序员要学会和使用其他编程语言的程序员合作.那么,让我来发布一个工具类--Java外接程序扩展包,并将相应算法发布.Java ...