Plist文件的操作

来源:互联网 发布:软件授权使用协议书 编辑:程序博客网 时间:2024/05/16 14:23

plist文件是苹果自带的配置文件 主要是以xml的格式进行匹配的

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

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


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


import UIKitclass 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 UIKitclass 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.    }}




0 0