在swift 2.1中写/读Plist

时间:2016-05-12 10:05:58

标签: ios swift2 plist

尝试将addObject保存到plist内的数组但不知道如何操作...

Plist的结构如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Location</key>
    <array>
        <string>test</string>
        <string>test2</string>
    </array>
    <key>UserID</key>
    <array>
        <string>use1</string>
        <string>user2</string>
    </array>
</dict>
</plist>

这就是我使用的:

从Documents目录中读取plist工作正常.... 在我的viewDidLoad

 guard
            let fileURL =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first?.URLByAppendingPathComponent("MyPlist.plist")
            else { fatalError("Unable to get file") }

        let dict = NSDictionary(contentsOfURL: fileURL)
        locationArray = dict?.objectForKey("Location") as! NSMutableArray
        userIDArray = dict?.objectForKey("UserID") as! NSMutableArray

在我的保存功能中:

 guard
        let fileURL =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first?.URLByAppendingPathComponent("MyPlist.plist")
        else { fatalError("Unable to get file") }

    let dict = NSMutableDictionary(contentsOfURL: fileURL)

    dict?.setObject(userDefaultLocationTextField.text!, forKey: "Location")
    dict?.writeToURL(fileURL, atomically: true)

但似乎没有任何东西写在我的plist中......

感谢。

2 个答案:

答案 0 :(得分:2)

而不是简单的NSDictionary(contentsOfURL: fileURL)我建议使用NSPropertyListSerialization类。

由于Swift的值类型语义,您需要在更改其中一个数组后更新根字典,例如

guard
  let fileURL =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first?.URLByAppendingPathComponent("MyPlist.plist"),
  fileData = NSData(contentsOfURL: fileURL)
  else { fatalError("Unable to get file") }

do {
  var dict = try NSPropertyListSerialization.propertyListWithData(fileData, options: .MutableContainersAndLeaves, format: nil) as! [String:AnyObject]

  var locationArray = dict["Location"] as! [String]
  locationArray.append(userDefaultLocationTextField.text!)
  dict["Location"] = locationArray // this line is crucial to update the enclosing dictionary

  let outputData = try NSPropertyListSerialization.dataWithPropertyList(dict, format: NSPropertyListFormat.XMLFormat_v1_0, options: 0)
  try outputData.writeToURL(fileURL, options: .AtomicWrite)
} catch let error as NSError {
  print(error)
}

答案 1 :(得分:0)

这对我来说似乎正在起作用: 你认为这是对的吗?

 guard
            let fileURL =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first?.URLByAppendingPathComponent("MyPlist.plist")
            else { fatalError("Unable to get file") }

        let dict = NSMutableDictionary(contentsOfURL: fileURL)
        locationArray = dict?.objectForKey("Location") as! NSMutableArray
        locationArray.addObject(userDefaultLocationTextField.text!)


        dict?.setObject(locationArray, forKey: "Location")
        dict?.writeToURL(fileURL, atomically: true)
        print(dict)