- 類型:Mac應用軟件大。2.45G語言:英文 評分:10.0
- 標簽:
立即下載
在Xcode中建立一個iOS項目后,會自己產生一個.plist文件,點擊時會看見它顯示的是類似于excel表格:

但是,如果打開方式選擇Source Code,你會看見它其實是一個xml文件。
我們會做一個小例子,在這個例子中我們自己建立一個plist文件并填入數據,然后運行時讀取這個plist文件,并將數據填寫在界面上。
首先要知道讀取plist文件的方法,一般來說,使用代碼
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"listFileName" ofType:@"plist"];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
已經足夠了,此時可以使用NSLog例程查看array和dictionary的內容。不過,有時候受plist文件內容的限制,array內容可能為空。
其實,用dictionary就已經足夠了,在下面的例子里我們也只用dictionary。
1、運行Xcode4.2,新建一個Single View Application,名稱為ReadPlistFile,其他設置如下圖:

2、新建我們自己的plist文件:
File —> New —> New File,選擇Mac OS X下的Property List

文件名為 customInfo,Group選擇Supporting Files。
3、單擊新建的customInfo.plist,我們添加數據,如下圖:

注意,Type一項的類型,選擇的是Dictionary,以Source Code打開,顯示如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Student</key>
<dict>
<key>Name</key>
<string>Yang</string>
<key>Sex</key>
<string>Male</string>
<key>Num</key>
<string>SX_010</string>
</dict>
<key>Mentor</key>
<dict>
<key>Name</key>
<string>Gu</string>
<key>Sex</key>
<string>Male</string>
</dict>
</dict>
</plist>
4、為視圖添加控件:
單擊BIDViewController.xib,打開IB,拖幾個控件上去,并設置好布局,如下圖:

上圖中所有的控件都是Label,并設置了字體大小。
5、接下來就是映射唄,把五個灰色的Label都映射到BIDViewController.h文件中,類型都死OutLet,名稱依次是stuName,stuSex,stuNum,mtName,mtSex。

6、單擊BIDViewController.m,在viewDidLoad方法中的[super viewDidLoad]之后添加如下代碼:
//首先讀取studentInfo.plist中的數據
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"customInfo" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
//將學生信息填入視圖
NSDictionary *tmpInfo = [dictionary objectForKey: @"Student"];
self.stuName.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Name"]];
self.stuSex.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Sex"]];
self.stuNum.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Num"]];
//將導師信息寫入視圖
tmpInfo = [dictionary objectForKey: @"Mentor"];
self.mtName.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Name"]];
self.mtSex.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Sex"]];
7、運行,查看效果:

iphone:plist的讀寫存代碼示例
PSE: collapse! important; HEIGHT: auto! important; TEXT-ALIGN: left! important; outline: 0px">01 | - (void)viewDidLoad { |
04 | NSString *path = [[NSBundle mainBundle] pathForResource:@”demo” ofType:@”plist”]; |
05 | NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; |
06 | NSLog(@”%d”,[data count]); |
09 | [data setObject:@"content" forKey:@"item4"]; |
12 | NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); |
13 | path = [paths objectAtIndex:0]; |
16 | NSString *filename=[path stringByAppendingPathComponent:@"test.plist"]; |
18 | [data writeToFile:filename atomically:YES]; |