1.建立plist文件
-(void)creatPlist{
//1.建立plist文件的路徑(plist文件要存儲的本地路徑)
NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/myPlist.plist"];
NSLog(@"本地路徑是:%@",filePath);
//2.準備存儲的數據
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setValue:@"value1" forKey:@"key1"];
//value爲字符串
[dic setValue:@[@"str1",@"str2",@"str3"] forKey:@"key2"];
//value爲數組
[dic setValue:@{@"k1":@"v1",@"k2":@"v2"} forKey:@"key3"];
//value爲字典
//3.將準備好的數據存儲到plist文件中
[dic writeToFile:filePath atomically:YES];
//**對數據進行修改
[dic setObject:@"劉志平" forKey:@"key1"];
[dic writeToFile:filePath atomically:YES];
//覆蓋修改前的數據
}
2.讀取plist文件
-(void)readPlist{
//第一種狀況:讀取本地存儲的plist文件
NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/myPlist.plist"];
//要讀取的plist文件的本地路徑
NSDictionary *dic1 = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSLog(@"dic1 = %@",dic1);
//第二種狀況:讀取工程內的plist文件
NSString *path = [[NSBundle mainBundle]pathForResource:@"Info" ofType:@"plist"];
NSDictionary *dic2 = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"dic2 = %@",dic2);
}