西西軟件園多重安全檢測(cè)下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁(yè)編程開發(fā)其它知識(shí) → iOS開發(fā)讀取plist文件、iphone中plist文件的讀寫存

iOS開發(fā)讀取plist文件、iphone中plist文件的讀寫存

相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來源:西西整理時(shí)間:2012/6/28 17:44:27字體大。A-A+

作者:佚名點(diǎn)擊:2326次評(píng)論:0次標(biāo)簽: Xcode

xcode 6.1.1 for Mac官方正式版
  • 類型:Mac應(yīng)用軟件大。2.45G語(yǔ)言:英文 評(píng)分:10.0
  • 標(biāo)簽:
立即下載

在Xcode中建立一個(gè)iOS項(xiàng)目后,會(huì)自己產(chǎn)生一個(gè).plist文件,點(diǎn)擊時(shí)會(huì)看見它顯示的是類似于excel表格:

但是,如果打開方式選擇Source Code,你會(huì)看見它其實(shí)是一個(gè)xml文件。

我們會(huì)做一個(gè)小例子,在這個(gè)例子中我們自己建立一個(gè)plist文件并填入數(shù)據(jù),然后運(yùn)行時(shí)讀取這個(gè)plist文件,并將數(shù)據(jù)填寫在界面上。

首先要知道讀取plist文件的方法,一般來說,使用代碼

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"listFileName" ofType:@"plist"];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

已經(jīng)足夠了,此時(shí)可以使用NSLog例程查看array和dictionary的內(nèi)容。不過,有時(shí)候受plist文件內(nèi)容的限制,array內(nèi)容可能為空。

其實(shí),用dictionary就已經(jīng)足夠了,在下面的例子里我們也只用dictionary。

1、運(yùn)行Xcode4.2,新建一個(gè)Single View Application,名稱為ReadPlistFile,其他設(shè)置如下圖:

2、新建我們自己的plist文件:

File —> New —> New File,選擇Mac OS X下的Property List

文件名為 customInfo,Group選擇Supporting Files。

3、單擊新建的customInfo.plist,我們添加數(shù)據(jù),如下圖:

注意,Type一項(xiàng)的類型,選擇的是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,拖幾個(gè)控件上去,并設(shè)置好布局,如下圖:

上圖中所有的控件都是Label,并設(shè)置了字體大小。

5、接下來就是映射唄,把五個(gè)灰色的Label都映射到BIDViewController.h文件中,類型都死OutLet,名稱依次是stuName,stuSex,stuNum,mtName,mtSex。

6、單擊BIDViewController.m,在viewDidLoad方法中的[super viewDidLoad]之后添加如下代碼:

//首先讀取studentInfo.plist中的數(shù)據(jù)
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"customInfo" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    
//將學(xué)生信息填入視圖
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"]];
    
//將導(dǎo)師信息寫入視圖
tmpInfo = [dictionary objectForKey: @"Mentor"];
self.mtName.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Name"]];
self.mtSex.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Sex"]];

7、運(yùn)行,查看效果:



iphone:plist的讀寫存代碼示例

PSE: collapse! important; HEIGHT: auto! important; TEXT-ALIGN: left! important; outline: 0px">
01- (void)viewDidLoad {
02 
03//讀取plist
04NSString *path = [[NSBundle mainBundle] pathForResource:@”demo” ofType:@”plist”];
05NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
06NSLog(@”%d”,[data count]);
07 
08//添加一項(xiàng)內(nèi)容
09[data setObject:@"content" forKey:@"item4"];
10 
11//獲取應(yīng)用程序沙盒的Documents目錄
12NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
13path = [paths objectAtIndex:0];
14 
15//得到完整的文件名
16NSString *filename=[path stringByAppendingPathComponent:@"test.plist"];
17 
18[data writeToFile:filename atomically:YES];
19[data release];
20[super viewDidLoad];
21}
    mac軟件
    (69)mac軟件
    現(xiàn)在是越做越好,漂亮極簡(jiǎn)的外觀讓喜歡的人越來越多。當(dāng)然很多用戶因?yàn)橄矚g買回來了,可是由于剛接觸,新系統(tǒng)使用起來還是遇到了各種煩人的事情,不得不在上用上了,這是一件悲哀的事情。這里西西給大家精選出了一些的實(shí)用教程和精品軟件,讓新用戶能對(duì)系統(tǒng)有個(gè)從淺到深的認(rèn)識(shí),讓老用戶不用再到處找軟件了。...更多>>
    • Mac版快播1.1.26 官方正式版[dmg]

      11-02 / 8.2M

      推薦理由:快播,無(wú)所不播,屌絲宅男必備神器,干嘛用的呢,你懂的,網(wǎng)上在線看片的。現(xiàn)針對(duì)高帥富果機(jī)用戶推出Mac版快
    • Adobe Flash Player for Mac OS X2

      02-07 / 18.5M

      推薦理由:AdobeFlashPlayer支持MacOSX系統(tǒng)的專用版.AdobeFlashPlayer最初設(shè)計(jì)目的為播放2維向量動(dòng)量,但至此之后成為
    • Mac讀寫NTFS(Paragon NTFS for Mac

      12-24 / 10.2M

      推薦理由:這個(gè)工具對(duì)于黑蘋果很有幫助,電腦上可以安裝win7 和 Mac 雙系統(tǒng),并能讀寫win7上的文件。Mac OS X 對(duì)NTFS—
    • 迅雷7 for macv3.0.1 官方最新版

      12-16 / 15.4M

      推薦理由:Mac迅雷是迅雷網(wǎng)絡(luò)針對(duì)MacOS系統(tǒng)推出的專業(yè)下載工具,是迅雷在多終端的重要布局。Mac版迅雷繼承了迅雷軟件輕
    • 吉吉影音mac版v1.0 官方最新版

      08-13 / 19.9M

      推薦理由:吉吉影音是繼快播之后的一款p2p影音播放神器,擁有網(wǎng)絡(luò)播放和本地播放兩種。主要特色還是在于資源廣,現(xiàn)在廣
    • 蘋果電腦JAVA運(yùn)行環(huán)境(Java SE 6 f

      04-04 / 74.0M

      推薦理由:蘋果剛剛發(fā)布了OS X版Java運(yùn)行環(huán)境更新,將Java SE 6版本更新至1.6.0_31。這款升級(jí)補(bǔ)丁被稱為Java for Mac

    相關(guān)評(píng)論

    閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無(wú)聊無(wú)聊

    熱門評(píng)論

    最新評(píng)論

    發(fā)表評(píng)論 查看所有評(píng)論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過審核才能顯示)