Swift - .plist文件数据的读取和存储
1
2
3
|
let array = NSArray (objects: "hangge.com" , "baidu.com" , "google.com" , "163.com" , "qq.com" ) let filePath: String = NSHomeDirectory () + "/Documents/webs.plist" array.writeToFile(filePath, atomically: true ) |
进入目录打开webs.plist,可以看到生成的数据如下:

1
2
3
4
5
6
7
8
9
10
11
|
<? 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" > < array > < string >hangge.com</ string > < string >baidu.com</ string > < string >google.com</ string > < string >163.com</ string > < string >qq.com</ string > </ array > </ plist > |

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import UIKit class ViewController : UIViewController , UITableViewDelegate , UITableViewDataSource { @IBOutlet weak var tableView: UITableView ! var webs: NSArray ? override func viewDidLoad() { super .viewDidLoad() webs = NSArray (contentsOfFile: NSHomeDirectory () + "/Documents/webs.plist" ) self .tableView!.delegate = self self .tableView!.dataSource = self //创建一个重用的单元格 self .tableView!.registerClass( UITableViewCell . self , forCellReuseIdentifier: "cell0" ) } func tableView(tableView: UITableView , numberOfRowsInSection section: Int ) -> Int { return webs!.count } func tableView(tableView: UITableView , cellForRowAtIndexPath indexPath: NSIndexPath ) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier( "cell0" , forIndexPath: indexPath) as UITableViewCell let url = webs![indexPath.row] as ! String cell.textLabel?.text = url return cell } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
let myArray = [ [ [ "name" : "航歌" , "url" : "hangge.com" ], [ "name" : "百度" , "url" : "baidu.com" ], [ "name" : "google" , "url" : "google.com" ] ], [ [ "name" : "163" , "url" : "163.com" ], [ "name" : "QQ" , "url" : "qq.com" ] ] ] //声明一个字典 let filePath: String = NSHomeDirectory () + "/Documents/webs.plist" NSArray (array: myArray).writeToFile(filePath, atomically: true ) print (filePath) |
进入目录打开webs.plist,可以看到生成的数据如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<? 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" > < array > < array > < dict > < key >name</ key > < string >航歌</ string > < key >url</ key > < string >hangge.com</ string > </ dict > < dict > < key >name</ key > < string >百度</ string > < key >url</ key > < string >baidu.com</ string > </ dict > < dict > < key >name</ key > < string >google</ string > < key >url</ key > < string >google.com</ string > </ dict > </ array > < array > < dict > < key >name</ key > < string >163</ string > < key >url</ key > < string >163.com</ string > </ dict > < dict > < key >name</ key > < string >QQ</ string > < key >url</ key > < string >qq.com</ string > </ dict > </ array > </ array > </ plist > |
下面把webs.plist文件中数据读取出来,并在表格里分区显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import UIKit class ViewController : UIViewController , UITableViewDelegate , UITableViewDataSource { @IBOutlet weak var tableView: UITableView ! var webs: NSArray ? override func viewDidLoad() { super .viewDidLoad() webs = NSArray (contentsOfFile: NSHomeDirectory () + "/Documents/webs.plist" ) self .tableView!.delegate = self self .tableView!.dataSource = self //创建一个重用的单元格 self .tableView!.registerClass( UITableViewCell . self , forCellReuseIdentifier: "cell0" ) } // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的头部 func tableView(tableView: UITableView , titleForHeaderInSection section: Int )-> String ? { return " " ; } //在本例中,有2个分区 func numberOfSectionsInTableView(tableView: UITableView ) -> Int { return webs!.count; } //每个分区的记录数 func tableView(tableView: UITableView , numberOfRowsInSection section: Int ) -> Int { let data = self .webs?[section] return data!.count } //每个单元格创建和内容赋值 func tableView(tableView: UITableView , cellForRowAtIndexPath indexPath: NSIndexPath ) -> UITableViewCell { let cell = UITableViewCell (style: UITableViewCellStyle . Value1 , reuseIdentifier: "cell0" ) let data = self .webs?[indexPath.section] let url = data![indexPath.row][ "url" ] as ! String let name = data![indexPath.row][ "name" ] as ! String cell.textLabel?.text = name cell.detailTextLabel?.text = url return cell } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() } } |
原文出自:www.hangge.com 转载请保留原文链接:http://www.hangge.com/blog/cache/detail_888.html
Swift - .plist文件数据的读取和存储的更多相关文章
- CSV文件数据如何读取、导入、导出到新的CSV文件中以及CSV文件的创建
CSV文件数据如何读取.导入.导出到新的CSV文件中以及CSV文件的创建 一.csv文件的创建 (1)新建一个文本文档: 打开新建文本文档,进行编辑. 注意:关键字与关键字之间用英文半角逗号隔开.第一 ...
- C语言进行csv文件数据的读取
C语言进行csv文件数据的读取: #include <stdio.h> #include <string.h> #include <malloc.h> #inclu ...
- HDF 文件数据的读取
http://www.cams.cma.gov.cn/cams_973/cheres_docs/cheres_doc_sat.modis.1b.html一. HDF文件格式 1.概述 HDF 是美国国 ...
- Python之TensorFlow的数据的读取与存储-2
一.我们都知道Python由于GIL的原因导致多线程并不是真正意义上的多线程.但是TensorFlow在做多线程使用的时候是吧GIL锁放开了的.所以TensorFlow是真正意义上的多线程.这里我们主 ...
- 加载plist文件数据的方法
这个pilist文件最外面的是一个数组,数组中每一个item是一个字典,我们的目的就是为了取到每一个item字典中的内容数据 下面看代码举例 //加载数组 - (void)handleData { / ...
- linux使用shell 进行文件数据的读取与排序
题目 shell脚本语言编写一个从键盘输入10名学生(含自己)的姓名. 性别.学号和家庭住址,然后按照学号排序,并将排序后的结果在屏幕上按对齐 的方式打印输出的程序. 代码 读入数据 数据排序(这里用 ...
- ios本地文件内容读取,.json .plist 文件读写
ios本地文件内容读取,.json .plist 文件读写 本地文件.json .plist文件是较为常用的存储本地数据的文件,对这些文件的操作也是一种常用的基础. 本文同时提供初始化变量的比较标准的 ...
- 黑马程序员——读取Plist文件
-iOS培训,iOS学习-------型技术博客.期待与您交流!------------ 读取Plist文件 一:新建一个plist文件,并将plist文件数据填入plist文件中,这里pli ...
- Pandas_数据读取与存储数据(全面但不精炼)
Pandas 读取和存储数据 目录 读取 csv数据 读取 txt数据 存储 csv 和 txt 文件 读取和存储 json数据 读取和存储 excel数据 一道练习题 参考 Numpy基础(全) P ...
随机推荐
- gdb调试相关
GDB调试及其调试脚本的使用返回脚本百事通一.GDB调试 1.1. GDB 概述 GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具.或许,各位比较喜欢那种图形界面方式的,像VC.BCB等 ...
- 高级UNIX环境编程13 守护进程
linux下,keventd守护进程为内核中运行的执行的函数提供进程上下文 bdflush,kupdated将高速缓存中的数据冲洗到磁盘上
- Qt浅译:JSON Support in Qt(JSON只有六种数据类型)
JSON Support in Qt Qt5之后开始提供对处理JSON数据的支持,JSON是一种Interter数据交换的数据格式. JSON 用于存储结构化的数据,JSON有6种基本数据类型 ...
- 小米2s的座充,看看这个是什么芯片? - 电池&综合DIY(Flashlight Electronics-Batteries Include - 手电大家谈-手电筒爱好者之家
小米2s的座充,看看这个是什么芯片? - 电池&综合DIY(Flashlight Electronics-Batteries Include - 手电大家谈-手电筒爱好者之家 小米2s的座充, ...
- HDU 4734 F(x) 2013 ACM/ICPC 成都网络赛
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4734 数位DP. 用dp[i][j][k] 表示第i位用j时f(x)=k的时候的个数,然后需要预处理下小 ...
- HDU 4569Special equations2012长沙邀请赛E题(数学知识)
Special equations Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Unity Notes调制粒子系统的颗粒的最大数目
Unity该粒子系统是很容易使用.这样的问题是在实际的过程中遇到的:以控制的粒子系统组件的动态需要可产生颗粒的最大数目. 看doc他说,有maxParticles控制.却没有这个开放的參数.仅仅能通过 ...
- css基础(二)
一.元素内容的字体属性 1.font-family 字体名称,例如:宋体,新罗马字体等 注意:1.不能使用偏僻的字体,要使用安全字体,如:arial;verdana;times new roman ...
- iOS 把图片从Mac本地添加到iOS Simulator中
[把图片从Mac本地添加到iOS Simulator中] 1. 把图片从Mac本机拖动到iOS Simulator中: 2. iOS Simulator会自动打开Safari去打开对应的图片,然后你用 ...
- BZOJ 1026: [SCOI2009]windy数( dp )
dp..dp(x, t) 表示共x位, 第x位为t有多少个windy数. 对答案差分, 我们只需统计1 ~ l-1和1 ~ r的windy数数量. 考虑如何计算[1, n]的答案 : 从最高位到最低位 ...