简体   繁体   English

json数据到文档目录中的plist文件,Swift 2.0

[英]json data to plist file in document directory , swift 2.0

I am trying to save the json data to plist file in document directory of the application.The code that i have used here is given below. 我正在尝试将json数据保存到应用程序文档目录中的plist文件中。我在这里使用的代码如下。 Is there anything wrong that im doing.Thanks in advance. 我在做什么有什么问题。谢谢。

  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"}]
        }

You can't use -writeToFile: , because it will write binary data and plist file since then will be broken. 您不能使用-writeToFile:因为它将写入二进制数据,并且plist文件将被破坏。
You need to use NSPropertyListSerialization class to serialize your object to plist which is XML with specific formatting requirements. 您需要使用NSPropertyListSerialization类将对象序列化为plist ,这是具有特定格式要求的XML。
Aside from documentation on class you can check next documentation section to strengthen your knowledge about plist serialization/deserialization: Creating Property Lists Programmatically 除了关于类的文档之外,您还可以查看下一个文档部分,以增强您对plist序列化/反序列化的了解:以编程方式创建属性列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM