繁体   English   中英

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

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

我正在尝试将json数据保存到应用程序文档目录中的plist文件中。我在这里使用的代码如下。 我在做什么有什么问题。谢谢。

  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 ,这是具有特定格式要求的XML。
除了关于类的文档之外,您还可以查看下一个文档部分,以增强您对plist序列化/反序列化的了解:以编程方式创建属性列表

暂无
暂无

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

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