简体   繁体   中英

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. 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.
You need to use NSPropertyListSerialization class to serialize your object to plist which is XML with specific formatting requirements.
Aside from documentation on class you can check next documentation section to strengthen your knowledge about plist serialization/deserialization: Creating Property Lists Programmatically

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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