Ios swift 2.0文档目录中plist文件的json数据
我试图将json数据保存到应用程序文档目录中的plist文件中。我做错什么了吗?谢谢你Ios swift 2.0文档目录中plist文件的json数据,ios,json,swift,Ios,Json,Swift,我试图将json数据保存到应用程序文档目录中的plist文件中。我做错什么了吗?谢谢你 let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) let documentsDirectory = dirPaths[0] let dataPath = documentsDirectory.stringByAppendingStrin
let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
.UserDomainMask, true)
let documentsDirectory = dirPaths[0]
let dataPath = documentsDirectory.stringByAppendingString("/flights.plist")
if !NSFileManager.defaultManager().fileExistsAtPath(dataPath) {
print("File is not present at location")
} else {
print("File Already Exist")
let arrrayOfValues : NSArray = result.valueForKey("list") as! NSArray
arrrayOfValues.writeToFile(dataPath, atomically: true)
//result.values(“list”) have the values like [{“name” : “nameValue”,”code”: "codeValue"}]
}
您不能使用
-writeToFile:
,因为它将写入二进制数据,此后plist文件将被破坏。您需要使用
NSPropertyListSerialization
类将对象序列化为plist,plist是具有特定格式要求的XML。除了类的文档外,您还可以查看下一个文档部分,以增强您对plist序列化/反序列化的了解:以编程方式创建属性列表什么是
sourcePath
?另外,您的逻辑可能是错误的:只有当文件已经存在时才保存它。@MartinR..我刚刚编辑了路径。。