ns string * path =[[ns bundle main bundle]bundle path];
ns string * final path =[path stringByAppendingPathComponent:@ " info . plist "];
ns dictionary * plist data =[[ns dictionary withcontentsofile:final path]retain];
version label =[[UILabel alloc]initWithFrame:CGRectMake(100,100,60,25)];
//例如
version label . background color =[ui color clear color];
version label . text color =[ui color white color];
version label . font =[ui font system font of size:10];
ns string * version string =[ns string string with format:@ " v % @ ",[plist data objectForKey:@ " CFBundleVersion "]];
version label . text = version string;
[self . view add subview:version label];
註意上面加粗的部分,可以看到程序是如何讀取字典類型的數據並從key中獲取值的。
Plist文件是壹個標準的xml文件,可以很容易地在cocoa中使用。以下是使用方法:以下代碼適用於Mac和iPhone。
寫入plist文件:
NSMutableDictionary * dict =[[NSMutableDictionary alloc]initwithcontentsofile:@ "/sample . plist "];
[dict set object:@ " Yes " for key:@ " restart springboard "];
[dict write to file:@ "/sample . plist " atomically:YES];
讀取plist文件:
NSMutableDictionary * dict =[[NSMutableDictionary alloc]initwithcontentsofile:@ "/sample . plist "];
ns string * object =[dict objectForKey:@ " restart springboard "];
//將plist文件讀入NSMutableDictionary。
dict plist =[[NSMutableDictionary alloc]initwithcontentsourl:[NSURL fileURLWithPath:path]];
[dictplist set object:@ " testOne " for key:@ " key 1 "];
NSLog([dictplist objectForKey:@ " key 1 "]);
NSArray * array =[[NSArray alloc]initWithObjects:@ " item 1 " ,@ " item 2 " ,@ " item 3 ",nil];
[dict plist set object:array forKey:@ " array item test "];
[dict plist write tofile:plist path atomically:YES];
ns string * name =[dictplist objectForKey:@ " name "];
NSLog(名稱);
ArrayList =[[NSMutableArray alloc]init with array:[dictplist objectForKey:@ " arrayitemtest "]];
//從NSMutableDictionary構建plist文件
NSArray * array =[[NSArray alloc]initWithObjects:@ " item 1 " ,@ " item 2 " ,@ " item 3 ",nil];
dict plist =[[NSMutableDictionary alloc]init];
[dictplist set object:@ " name one " forKey:@ " name "];
[dict plist set object:array forKey:@ " item "];
[dict plist write tofile:plist path atomically:YES];
//ArrayList =[[NSMutableArray alloc]init];
//[ArrayList add object:[dictplist objectForKey:@ " name "]];
ArrayList =[[NSMutableArray alloc]init with array:[dictplist objectForKey:@ " item "]];