一 ! 在工程里直接創(chuàng)建一個Plist 文件:
//獲取Plist 文件路徑
NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"ZTProPertyList" ofType:@"plist"];
//把plist 轉(zhuǎn)換為字典
NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithContentsOfFile:[ZTGlobalClass openProPertyList]];
//設(shè)置字典的值并保存
[dic setValue:@(indexPath.row) forKey:@"childMenu"];
[dic writeToFile:[ZTGlobalClass openProPertyList] atomically:YES];
二逞刷! 創(chuàng)建plist在沙盒中
//建立文件管理
NSFileManager *fm = [NSFileManager defaultManager];
//找到Documents文件所在的路徑
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//取得第一個Documents文件夾的路徑
NSString *filePath = [path objectAtIndex:0];
//把TestPlist文件加入
NSString *plistPath = [filePath stringByAppendingPathComponent:@"XXX.plist"];
//開始創(chuàng)建文件
[fm createFileAtPath:plistPath contents:nil attributes:nil];
在寫入數(shù)據(jù)之前,需要把要寫入的數(shù)據(jù)先寫入一個字典中瓦糟,創(chuàng)建一個dictionary:
//創(chuàng)建一個字典
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2", nil];
//把數(shù)據(jù)寫入plist文件
[dic writeToFile:plistPath atomically:YES];
//刪除文件
[fm removeItemAtPath:plistPath error:nil];