国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS應用中存儲用戶設置的plist文件的創建與讀寫教程

2020-07-26 03:22:18
字體:
來源:轉載
供稿:網友

    在做iOS開發時,經常用到到plist文件,  那plist文件是什么呢? 它全名是:Property List,屬性列表文件,它是一種用來存儲串行化后的對象的文件。屬性列表文件的擴展名為.plist ,因此通常被稱為 plist文件。文件是xml格式的。
Plist文件通常用于儲存用戶設置,也可以用于存儲捆綁的信息

我們創建一個項目來學習plist文件的讀寫。

1、創建項目Plistdemo
項目創建之后可以找到項目對應的plist文件,打開如下圖所示:

201641890318772.png (611×296)

在編輯器中顯示類似與表格的形式,可以在plist上右鍵,用源碼方式打開,就能看到plist文件的xml格式了。

2、創建plist文件。
按command +N快捷鍵創建,或者File ―> New ―> New File,選擇Mac OS X下的Property List

201641890447160.jpg (728×491)

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

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

201641890508538.png (417×150)

注意,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,拖幾個控件上去,并設置好布局,如下圖:

201641890555870.png (442×571)

上圖中所有的控件都是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、運行,查看效果:

201641890616043.png (444×573)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泸溪县| 宝鸡市| 隆尧县| 东乡族自治县| 油尖旺区| 榆社县| 南阳市| 安岳县| 银川市| 周宁县| 襄汾县| 庆阳市| 阜宁县| 多伦县| 崇仁县| 凤阳县| 长海县| 保德县| 祁连县| 大安市| 麦盖提县| 白河县| 金乡县| 苍山县| 库伦旗| 荔浦县| 霍林郭勒市| 荣昌县| 乌兰县| 呼伦贝尔市| 双鸭山市| 韶山市| 高要市| 安西县| 芜湖市| 平昌县| 台江县| 关岭| 依兰县| 胶南市| 马山县|