①有時(shí)候進(jìn)行少量數(shù)據(jù)持久化存儲(chǔ)時(shí)候,可使用屬性列表 Plist 文件。
②如項(xiàng)目中要使用比較多的配置數(shù)據(jù),可生成 Plist 后再轉(zhuǎn)移到項(xiàng)目中
下面例子簡(jiǎn)單實(shí)現(xiàn)對(duì)于在沙盤下的 Plist 文件進(jìn)行增刪查改操作。

保存

更改

刪除
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var userNumber: UITextField!
@IBOutlet weak var userName: UITextField!
@IBOutlet weak var userAge: UITextField!
var filePath = "" //文件路徑
override func viewDidLoad() {
super.viewDidLoad()
self.createPlistFile()
}
//判斷是否存在該plist文件,不存在則創(chuàng)建
func createPlistFile() {
//獲取路徑
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
filePath = path.stringByAppendingString("/data666.plist")
//###注意加橫桿“/”,否則真機(jī)測(cè)試崩潰,因?yàn)闊o法寫入####
if !NSFileManager.defaultManager().fileExistsAtPath(filePath) {
let rootDic = NSMutableDictionary()
rootDic.writeToFile(filePath, atomically: true)
}
}
//遍歷數(shù)據(jù)
func findPlistFileData() -> Bool {
if let dicData = NSMutableDictionary(contentsOfFile: filePath) {
for (k,_) in dicData {
// 判斷是否存在相同的學(xué)號(hào)
if userNumber.text! == (k as! String) {
return true
}
}
}
return false
}
//保存數(shù)據(jù)
func savePlistData() {
let rootDic = NSMutableDictionary(contentsOfFile: filePath) //根
let userDataDic = NSMutableDictionary() //二級(jí)
userDataDic.setObject(userAge.text!, forKey: "age")
userDataDic.setObject(userName.text!, forKey: "name")
rootDic!.setObject(userDataDic, forKey: userNumber.text!)
rootDic!.writeToFile(filePath, atomically: true)
print("保存成功")
}
//刪除數(shù)據(jù)
func deletePlistData() {
let rootDic = NSMutableDictionary(contentsOfFile: filePath) //根
rootDic!.removeObjectForKey(userNumber.text!) //二級(jí)
rootDic!.writeToFile(filePath, atomically: true)
print("刪除成功")
}
// MARK: - Action
// 保存按鈕
@IBAction func saveData(sender: UIButton) {
//門衛(wèi) 檢查
guard findPlistFileData() == false else {
let alert = UIAlertController(title: "學(xué)號(hào)已存在", message: "是否要覆蓋當(dāng)前學(xué)號(hào)所有內(nèi)容", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "是的", style: .Default, handler: { (action) -> Void in
self.savePlistData() //保存數(shù)據(jù)
}))
alert.addAction(UIAlertAction(title: "否", style: .Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
return
}
self.savePlistData() //保存數(shù)據(jù)
let alert = UIAlertController(title: "保存成功", message: "", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
// 刪除按鈕
@IBAction func deleteData(sender: UIButton) {
//門衛(wèi) 檢查
guard findPlistFileData() == true else {
let alert = UIAlertController(title: "學(xué)號(hào)不存在", message: "請(qǐng)查看是否輸入有誤", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "確定", style: .Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
return
}
let alert = UIAlertController(title: "刪除成功", message: "", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "確定", style: .Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
self.deletePlistData() //刪除數(shù)據(jù)
}
// 查找按鈕
@IBAction func findData(sender: UIButton) {
guard findPlistFileData() == true else {
print("該學(xué)號(hào)不存在")
return
}
if let dicData = NSMutableDictionary(contentsOfFile: filePath) {
for (k,v) in dicData {
if userNumber.text! == (k as! String) {
let dic2 = v as! NSDictionary
print("該學(xué)號(hào)已存在")
for (k1, v2) in dic2 {
print("\(k1): \(v2)")
}
}
}
}
}
}