NSString
*path = [[NSBundle mainBundle] pathForResource:@
"文件名"
ofType:@
"plist"
];
//
文件数据类型是array
NSArray
*array=[NSArray arrayWithContentsOfFile:path];
//文件数据类型是*dictionary
NSDictionary
*dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
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对象,然后进行读或者写操作。
针对同一个关键字对应的对象或者数据,可以对它进行重写,重写之后关键字就对应新的对象或者数据,旧的对象或者数据会被自动清理。
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];
你可以使用以下代码强制保存[[NSUserDefaults standardUserDefaults] synchronize];