讀取和寫入plist檔案

2021-06-05 22:29:06 字數 1958 閱讀 3903

plist檔案是標準的xml檔案,在cocoa中可以很簡單地使用。這裡介紹一下使用方法:

以下**在mac和iphone中均適用。

寫入plist檔案:

nsmutabledictionary

* dict

=[ [

nsmutabledictionary

alloc

]initwithcontentsoffile

:@"/sample.plist"

];

[ dict

setobject

:@"yes"

forkey

:@"restartspringboard"

];

[ dict

writetofile

:@"/sample.plist"

atomically

:yes

];

讀取plist檔案:

nsmutabledictionary

* dict

=[ [

nsmutabledictionary

alloc

]initwithcontentsoffile

:@"/sample.plist"

];

nsstring

*object

=[ dict

objectforkey

:@"restartspringboard"

];

//讀plist 檔案  到 nsmutabledictionary 中

dictplist = [[nsmutabledictionary alloc ] initwithcontentsofurl:[nsurl fileurlwithpath:path]];

[dictplist setobject:@"testone" forkey:@"key1"];

nslog([dictplist objectforkey:@"key1"]);

nsarray *array = [[nsarray alloc] initwithobjects:@"item1",@"item2",@"item3",nil];

[dictplist setobject:array forkey:@"arrayitemtest"];

[dictplist writetofile:plistpath atomically:yes];

nsstring *name = [dictplist objectforkey:@"name" ];

nslog(name);

arraylist = [[nsmutablearray alloc] initwitharray:[dictplist objectforkey:@"arrayitemtest"]];

//從nsmutabledictionary 中構建 plist 檔案

nsarray *array = [[nsarray alloc] initwithobjects:@"item1",@"item2",@"item3",nil];

dictplist = [[nsmutabledictionary alloc ] init];

[dictplist setobject:@"nameone" forkey:@"name"];

[dictplist setobject:array forkey:@"item"];

[dictplist writetofile:plistpath atomically:yes];

//arraylist = [[nsmutablearray alloc] init];

//[arraylist addobject:[dictplist objectforkey:@"name"]];

arraylist = [[nsmutablearray alloc] initwitharray:[dictplist objectforkey:@"item"]];

建立 寫入 讀取 刪除 Plist 檔案中資料

建立plist檔案 找到第乙個documents資料夾所在的路徑 nsstring cachepatch nssearchpathfordirectoriesindomains nscachesdirectory,nsuserdomainmask,yes 0 把procucts檔案加入 produc...

檔案讀取和寫入

open 返回乙個檔案物件,open filename,mode f open workfile w 第乙個引數是包含檔名的字串,第二個引數可以是包含一些字元的字串 r 僅讀取檔案,w 僅寫入檔案,a 開啟檔案以進行新增的模式 r 開啟檔案進行讀取和寫入,模式引數是可選的。r 如果省略,將被假定。b...

檔案寫入和讀取

最近在提高自己程式設計能力,拿一些現實的小指令碼練下。該指令碼為python語言,主要涉及模組os。功能 將控制台輸入文字逐行儲存,和讀取。輸入逐行儲存 import os filename input please enter file name file open filename,w whil...