动态创建 Plist 文件
生活随笔
收集整理的這篇文章主要介紹了
动态创建 Plist 文件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
簡(jiǎn)介
Property List,屬性列表文件,它是一種用來(lái)存儲(chǔ)串行化后的對(duì)象的文件。屬性列表文件的擴(kuò)展名為.plist?,因此通常被稱為?plist文件,文件是xml格式的。
?
寫入plist文件
在開發(fā)過(guò)程中,有時(shí)候需要把程序的一些配置保存下來(lái),或者游戲數(shù)據(jù)等等。 這時(shí)候需要寫入Plist數(shù)據(jù)。寫入的plist文件會(huì)生成在對(duì)應(yīng)程序的沙盒目錄里。
1 -(void)triggerStorage 2 { 3 // displayLabel.text = textInput.text; 4 5 NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 6 NSString *path=[paths objectAtIndex:0]; 7 NSString *filename=[path stringByAppendingPathComponent:@"test.plist"]; //獲取路徑 8 9 NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename]; //讀取數(shù)據(jù) 10 NSLog(@"dic2 is:%@",dic2); 11 12 //創(chuàng)建一個(gè)dic,寫到plist文件里 13 NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:@"displayLabel.text",@"IP",nil]; //寫入數(shù)據(jù) 14 // 或者 [dic setObject:@"add some content" forKey:@"c_key"]; 15 [dic writeToFile:filename atomically:YES]; 16 17 }?
?
讀取文件
1 - (void)readData { 2 NSMutableArray *resultData; 3 // 獲取應(yīng)用程序沙盒的Documents目錄 4 NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 5 NSString *path=[paths objectAtIndex:0]; 6 NSLog(@"path = %@",path); 7 // 得到完整的文件名 8 NSString *filename=[path stringByAppendingPathComponent:@"test.plist"]; 9 10 //讀文件 11 NSDictionary* dic2 = [NSDictionary dictionaryWithContentsOfFile:filename]; 12 NSLog(@"dic is:%@",dic2); 13 if(dic2 == nil) 14 { 15 //1. 創(chuàng)建一個(gè)plist文件 16 NSFileManager* fm = [NSFileManager defaultManager]; 17 [fm createFileAtPath:filename contents:nil attributes:nil]; 18 } 19 else 20 { 21 resultData=[dic2 objectForKey:@"IP"]; 22 if([dic2 count] > 0) 23 { 24 // displayLabel.text = resultData; 25 NSLog(@"讀取的值是:%@", resultData); 26 } 27 else 28 { 29 // displayLabel.text = @" "; 30 NSLog(@"沒(méi)有讀取到任何值!"); 31 } 32 } 33 }?
轉(zhuǎn)載于:https://www.cnblogs.com/EchoHG/p/8464743.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的动态创建 Plist 文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 做梦梦到车门掉了怎么回事
- 下一篇: 梦到家里有蛇是什么意思