//plist文件,即属性列表文件,是一种用来存储串行化后的对象的文件。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// 用代码创建plist属性列表文件
// 初始化一个可变字典对象,作为属性列表内容的容器
let dict:NSMutableDictionary = NSMutableDictionary()
// 设置内容,以键值对的形式添加、
dict.setObject("Peler", forKey: "Name" as NSCopying)
dict.setObject("35", forKey: "Age" as NSCopying)
// 生成文件的存储路径
let plistPath = Bundle.main.path(forResource: "demoPlist", ofType: "plist")
// 将可变字典对象写入到指定位置的属性列表文件
dict.write(toFile: plistPath!, atomically: true)
// 读取和解析Plist属性列表文件
// 读取属性列表文件,并转化为可变字典对象
let data:NSMutableDictionary = NSMutableDictionary.init(contentsOfFile: plistPath!)!
// 将字典对象转化为字符串对象
let message = data.description
let name = data["Name"]
let age = data["Age"]
print(message)
print(name)
print(age)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
swift4--代码创建并读取和解析Plist属性列表文件
猜你喜欢
转载自blog.csdn.net/weixin_41735943/article/details/81233481
今日推荐
周排行