- NSUserDefaults只能讀寫(xiě)Library/Preferences/<Application BundleIdentifier>.plist這個(gè) 文件
- PList文件是XML格式的,只能存放固定數(shù)據(jù)格式的對(duì)象
- PList文件支持的數(shù)據(jù)格式有NSString, NSNumber, Boolean, NSDate, NSData, * NSArray,和NSDictionary。其中,Boolean格式事實(shí)上以[NSNumber numberOfBool:YES/NO];這樣的形式表示。NSNumber支持float和int兩種格式陌凳。
1.創(chuàng)建PList文件
_path = [NSTemporaryDirectory()
stringByAppendingString:@"custom.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
}
if (![fileManager fileExistsAtPath:_path]) {
//使用NSMutableDictionary的寫(xiě)文件接口自動(dòng)創(chuàng)建一個(gè)
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"textField"] = @"hello,world!";
if (![dict writeToFile:_path atomically:YES]) {
NSLog(@"Error!!!");
}
2.寫(xiě)入PList文件
- (IBAction)saveData:(id)sender {
}
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"textField"] = _textField.text;
if (![dict writeToFile:_path atomically:YES]) {
NSLog(@"Error!!!");
}
3.讀取文件
{
NSMutableDictionary *dict = [NSMutableDictionary
dictionaryWithContentsOfFile:_path];
NSString *content = dict[@"textField"];
if (content && content.length > 0) {
_textField.text = content;
}
}