在MacOS项目中使用Swift管理plist,实现读写plist文件。
1. Jobs.swift
使用Jobs类管理Jobs.plist
// Jobs.swift
// Created by Cyril on 18/7/11.
// Copyright © 2018年 GJ. All rights reserved.
import Foundation
class Jobs: NSObject {
var RestoreSequence_1: Dictionary<String , AnyObject?> = [:]
var RestoreSequence_2: Dictionary<String , AnyObject?> = [:]
var RestoreSequence_3: Dictionary<String , AnyObject?> = [:]
let dModel = DataModel()
//构造方法
override init(){
super.init()
print("[Jobs] 初始化...")
onCreateData("Jobs.plist")
let d = dModel.loadData("Jobs", dFilePath: "Jobs.plist")
let skey:String = "RestoreStage"
print("查找\(skey)结果:\(dModel.searchValueByKey(d, skey: skey))")
}
func onCreateData(fileName: String){
print("[Jobs]: 开始创建plist文件")
//Document path 本地数据数据路径 .plist格式
let fileManager = NSFileManager.defaultManager()
let localDataPath:String = dModel.dataFilePath(fileName)
print("[Jobs]: " + localDataPath)
var isLocalDataExisted = fileManager.fileExistsAtPath(localDataPath)
print("isLocalDataExisted->\(isLocalDataExisted)")
//判断.plist文件是否存在
if isLocalDataExisted == false//文件不存在
{
fileManager.createFileAtPath(localDataPath, contents: nil, attributes: nil)//创建.plist文件
}
isLocalDataExisted = fileManager.fileExistsAtPath(localDataPath)
print("isLocalDataExisted->\(isLocalDataExisted)")
//设置root
let root = NSMutableDictionary()
//设置dictionay
root.setValue(
[
"RestoreSequence_1":[
"RestoreStage" : "P1",
"Input_Type" : "Uart_Diags",
"TempValues" : [
"MLBSN" : "#ProgramWriteToIt#",
"FGSN" : "#ProgramWriteToIt#",
"Mod#" : "#ProgramWriteToIt#",
"Regn" : "#ProgramWriteToIt#",
],
"Command_Device" : [
"TTPowerSupply" : "#TT治具上电命令#",
"MLBSN" : "syscfg print MLB#",
"Write_FGSN" : "sn #FGSN#",
"Print_FGSN" : "sn",
"Write_Mod" : "syscfg add Mod# #Mod##",
"Print_Mod" : "syscfg print Mod#",
"Write_Regn" : "syscfg add Regn #Regn#",
"Print_Regn" : "syscfg print Regn",
"LogCollection" : "#LogCollection#",
"Restart" : "#TT治具重新上电命令#"
],
"Command_Host" : [
"暂时为空" : "暂时为空"
]
],
"RestoreSequence_2":[
"RestoreStage" : "P2",
"Input_Type" : "Uart_iBoot",
"TempValues" : [
"MLBSN" : "#ProgramWriteToIt#",
"FGSN" : "#ProgramWriteToIt#",
"#暂时为空#" : "#暂时为空#",
],
"Command_Device" : "#暂时为空#",
"Command_Host" : [
"暂时为空" : "暂时为空"
]
],
"RestoreSequence_FG":[
"RestoreStage" : "FG",
"Input_Type" : "Uart_OS",
"TempValues" : [
"FGSN" : "#ProgramWriteToIt#",
"#暂时为空#" : "#暂时为空#",
],
"Command_Device" : "#暂时为空#",
"Command_Host" : [
"暂时为空" : "暂时为空"
]
],
], forKey: "Jobs")
//将root写入.plist文件
root.writeToFile(localDataPath, atomically: true)
isLocalDataExisted = fileManager.fileExistsAtPath(localDataPath)
if isLocalDataExisted == true{
print("[Jobs]: Jobs.plist文件创建完成")
}else{
print("[Jobs]: Jobs.plist文件创建失败")
}
/* try! fileManager.removeItemAtPath(localDataPath)//删除本地数据文件 */
}
}
2. DataModel.swift
// DataModel.swift
// Created by Cyril on 18/7/11.
// Copyright © 2018年 GJ. All rights reserved.
import Foundation
class DataModel: NSObject {
var userList = [UserInfo]()
// var keyArray: Dictionary = [Int: String] ()
//获取沙盒文件夹路径
func documentsDirectory()->String {
/* let paths = NSSearchPathForDirectoriesInDomains(
NSSearchPathDirectory.DocumentationDirectory,NSSearchPathDomainMask.UserDomainMask,true)
let documentsDirectory:String = paths.first! as String */
let path = "./Cyril/Anole"
return path
}
/**
获取数据文件地址
Eg:
fileName:userList.plist
*/
func dataFilePath (fileName: String)->String{
print(documentsDirectory().stringByAppendingString("/" + fileName))
return documentsDirectory().stringByAppendingString( "/\(fileName)")
}
/**
读取数据
Eg:
key: userList
dFilePath: userList.plist
*/
func loadData(key: String, dFilePath: String)-> NSDictionary{
//判断.plist文件是否存在
let fileManager = NSFileManager.defaultManager()
let isLocalDataExisted = fileManager.fileExistsAtPath(self.dataFilePath(dFilePath))
print("isLocalDataExisted->\(isLocalDataExisted)")
if isLocalDataExisted == false//文件不存在
{
fileManager.createFileAtPath(self.dataFilePath(dFilePath), contents: nil, attributes: nil)//创建.plist文件
}
let dicFromPList: NSDictionary? = NSDictionary(contentsOfFile: "/Users/coreos/Desktop/Cyril/Anole/Jobs_P1P2FG.plist")
print("文件 " + dFilePath + " 读取完成")
return dicFromPList!
}
/**
* 通过key查找在字典中的value值
*/
func searchValueByKey(dic: NSDictionary, skey: String)-> String{
var printHead = "[searchValueByKey]: "
if (dic.count == 0){
return printHead + "输入的参数字典为空"
}
//plist 四层结构遍历(尚有bug未更正)
for (key1, value1) in (dic as! NSMutableDictionary) {
// keyArray.updateValue(key as! String, forKey: 1)
print("第1层->字典 key \(key1) - 字典 value () \(value1)")
if(key1 as! String == skey){
print("\(printHead)\(skey)查找成功, 为根字典")
return "查找的\(skey)的值为\(value1)"
}
for (key2, value2) in (value1 as! NSMutableDictionary) {
print("第2层->字典 key \(key2) - 字典 value \(value2)")
if(key2 as! String == skey){
print("\(printHead)\(skey)查找成功, 在字典\(key1)下")
return "查找的\(skey)的值为\(value2)"
}
for (key3, value3) in (value2 as! NSMutableDictionary) {
print("第3层->字典 key \(key3) - 字典 value \(value3)")
if(key3 as! String == skey){
print("\(printHead)\(skey)查找成功, 在字典\(key2)下")
return "查找的\(skey)的值为\(value3)"
for (key4, value4) in (value3 as! NSMutableDictionary) {
print("第4层->字典 key \(key4) - 字典 value \(value4)")
if(key4 as! String == skey){
print("\(printHead)\(skey)查找成功, 在字典\(key3)下")
return "查找的\(skey)的值为\(value4)"
}
}
}
}
}
}
return "\(printHead)输入的参数字典为空"
}
/**
* 值类型判断
*/
func judgeValueType(dic: AnyObject) -> String{
if let dict1 = dic as? NSMutableDictionary{
return "NSMutableDictionary"
}else if let dict1 = dic as? NSDictionary{
return "NSDictionary"
}else if let dict1 = dic as? NSArray{
return "NSArray"
}else if let dict1 = dic as? NSValue{
return "NSValue"
}else{
return "\(dic)" //其他可能是字串类型
}
}
}