在plist文件中增删改查
生活随笔
收集整理的這篇文章主要介紹了
在plist文件中增删改查
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
plist文件是一種輕量級大數(shù)據(jù)存儲方式,它的全名為PropertyList,即屬性列表文件,它是一種用來存儲串行化后的對象的文件。Plist文件是以key-value的方式來存取數(shù)據(jù)的。
? ? ?創(chuàng)建plist文件可通過xcode在工程中創(chuàng)建,也可通過代碼來創(chuàng)建:
? ? ?1.plist文件的創(chuàng)建:
?
NSFileManager *manager = [NSFileManager defaultManager];//找到沙盒路徑下document所在的文件路徑NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];//創(chuàng)建user文件夾路徑NSString *directorypath = [path stringByAppendingPathComponent:@"user"];//創(chuàng)建文件夾[manager createDirectoryAtPath:directorypath withIntermediateDirectories:false attributes:nil error:nil];//判斷文件夾是否存在 [self exictPath:directorypath];//刪除文件夾 [manager removeItemAtPath:directorypath error:nil];[self exictPath:directorypath];//在document路徑下創(chuàng)建plist文件路徑NSString *filepath = [path stringByAppendingPathComponent:@"moxue.plist"];//創(chuàng)建文件[manager createFileAtPath:filepath contents:nil attributes:nil];//如果需要修改文件的相關屬性,可以通過NSFileManager得到文件的所有屬性,然后設置相應的attributes值來更改,attributes值為字典類型// NSLog(@"%@",[manager attributesOfItemAtPath:filepath error:nil]); [self exictPath:filepath];//移除文件 [manager removeItemAtPath:filepath error:nil];[self exictPath:filepath];?
? ? ? ?2.向plist文件添加數(shù)據(jù)
//創(chuàng)建可變字典,在其中添加數(shù)據(jù)NSMutableDictionary *mainDict = [[NSMutableDictionary alloc]init];//在rootdict下創(chuàng)建一個子字典,并添加數(shù)據(jù)NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];[dict setObject:@"liu" forKey:@"one"];[dict setObject:@"zhi" forKey:@"two"];//將創(chuàng)建的子字典添加到向plist傳輸數(shù)據(jù)的字典[mainDict setValue:dict forKey:@"item1"];//通過mainDict向plist寫入數(shù)據(jù) [mainDict writeToFile:filepath atomically:YES];//插入第二條子字典NSMutableDictionary *dict11 = [[NSMutableDictionary alloc]init];[dict11 setObject:@"liu" forKey:@"one"];[dict11 setObject:@"zhi" forKey:@"two"];[mainDict setValue:dict11 forKey:@"item2"];[mainDict writeToFile:filepath atomically:YES];? ? ? 3.在plist文件中刪除數(shù)據(jù)
//在plist列表中移除第二個字典[mainDict removeObjectForKey:@"item2"];[mainDict writeToFile:filepath atomically:YES];? ? ?4.查看plist文件中的數(shù)據(jù)
//得到plist列表中所有的數(shù)據(jù)并打印NSDictionary *lookdict = [NSDictionary dictionaryWithContentsOfFile:filepath];NSLog(@"lookdict == %@",lookdict);?
?
? ? ??
轉載于:https://www.cnblogs.com/moxuexiaotong/p/4918938.html
總結
以上是生活随笔為你收集整理的在plist文件中增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (转) vector的reserve和r
- 下一篇: 如何快速REPAIR TABLE