您的位置:首页 > 其它

Plist文件的操作

2015-11-22 10:12 411 查看
plist文件是苹果自带的配置文件 主要是以xml的格式进行匹配的

创建的时候 选择resource的plist来进行创建 他主要有两种类型 一种是字典类型的 还有一种是数组类型的

可以通过添加子元素来进行创建



通过数组来解析plist文件 将plist文件解析后,保存在一个数组中,随后将其输出即可 此时的plist文件一定要是数组类型的 否则无法输出

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//通过数组来进行解析
let arr = NSArray(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("data", ofType: "plist")!))
//输出数组
if let a = arr {
print(a)
}
else {
print("nil")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}



字典的解析 同样的道理 此时的plist文件也必须是字典类型的否则也无法输出 数据类型就是文章开始创建的data.plist文件

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//通过字典来进行解析
let dict = NSDictionary(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("data", ofType: "plist")!))
//输出字典
if let dict1  = dict{
print(dict1)
//输出字典的name属性所对应的值
var name = dict1["name"]
print("name: \(name!)")
}
else {
print("nil")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: