如何在SWIFT中将词典添加到Plist

时间:2015-06-05 06:05:51

标签: ios swift plist

我有一个类型字典如下

var favoriteGooglelocations = [Int:GoogleItems]()

其中GoogleItems是类。

class GoogleItems: NSObject {

    var name:String?
    var address:String?
    var distance : String?
    var googleLocation: CLLocation?
    var isStar : Bool?

}

我正在写字典:

var newLocation = GoogleItems()
newLocation.name = locations[sender.tag].name
newLocation.address = locations[sender.tag].address
newLocation.distance = locations[sender.tag].distance
favoriteGooglelocations[sender.tag] = newLocation

当我尝试编写这个字典时,它给出了编译时错误favoriteGooglelocations.writeToFile(path, atomically: true)

  

错误:> [Int:GoogleItems]没有名为的成员   将writeToFile

已更新: 我写的路径是

 let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)

 let documentDirectory = paths[0] as! String

 path = documentDirectory.stringByAppendingPathComponent("Favorites.plist")

如何将词典添加到PList,还有其他方法吗?

2 个答案:

答案 0 :(得分:1)

您需要将词典转换为NSDictionary,以便您可以使用writeToFile

尝试使用此

进行投射
let succeed = (favoriteGooglelocations as NSDictionary).writeToFile(path, atomically: true)

如果成功为假,请检查您的GoogleItems类,它应符合NSCoding

更新,这是我测试的代码,你可以参考它

import UIKit
import CoreLocation

设置GoogleItems符合NSCoding

class GoogleItems: NSObject,NSCoding {
override init() {}
var name:String?
var address:String?
var distance : String?
var googleLocation: CLLocation?
var isStar : Bool?
required init(coder aDecoder: NSCoder) {
    self.name = aDecoder.decodeObjectForKey("name") as? String
    self.address = aDecoder.decodeObjectForKey("address") as? String
    self.distance = aDecoder.decodeObjectForKey("distance") as? String
    self.googleLocation = aDecoder.decodeObjectForKey("googleLocation") as? CLLocation
    self.isStar = aDecoder.decodeObjectForKey("isStar") as? Bool
}
func encodeWithCoder(aCoder: NSCoder) {
    if name != nil{  aCoder.encodeObject(name, forKey: "name")}
    if address != nil{ aCoder.encodeObject(address, forKey: "address")}
    if distance != nil { aCoder.encodeObject(distance, forKey: "distance")}
    if googleLocation != nil { aCoder.encodeObject(googleLocation, forKey: "googleLocation")}
    if isStar != nil {aCoder.encodeBool(isStar!, forKey: "isStar")}
}
}

然后写入文件

    var favoriteGooglelocations = [Int:GoogleItems]()
    var item = GoogleItems()
    item.isStar = true
    item.name = "name"
    item.address = "address"
    item.googleLocation = nil
    item.distance = "23"
    favoriteGooglelocations[1] = item
    let dic = favoriteGooglelocations as NSDictionary
    let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    let documentDirectory = paths[0] as! String
    let  path = documentDirectory.stringByAppendingPathComponent("Favorites.plist")
    let succeed = NSKeyedArchiver.archiveRootObject(dic, toFile: path)
    println(succeed)

从文件中读取

     let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)

    let documentDirectory = paths[0] as! String

    let  path = documentDirectory.stringByAppendingPathComponent("Favorites.plist")
    let dic = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? [Int:GoogleItems]
    if(dic != nil){
        for (key,value) in dic!{
            let item = value
            println(item.name)
            println(item.address)
        }

    }

答案 1 :(得分:1)

更新: Xcode 7.2•Swift 2.1.1

您可以使用NSKeyedArchiver.archiveRootObject(),如下所示:

let dictionary:[String:String] = ["key1" : "value1", "key2":"value2", "key3":"value3"]

let documentDirectoryURL =  NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
let fileURL = documentDirectoryURL.URLByAppendingPathComponent("dictionary.plist")
if NSKeyedArchiver.archiveRootObject(dictionary, toFile: fileURL.path!) {
    print(true)
}

if let loadedDic = NSKeyedUnarchiver.unarchiveObjectWithFile(fileURL.path!) as? [String:String] {
    print(loadedDic)   // "["key1": "value1", "key2": "value2", "key3": "value3"]\n"
}

如果GoogleItems符合NSCoding

if NSKeyedArchiver.archiveRootObject(favoriteGooglelocations, toFile: fileURL.path!) {
    if let loadedDic = NSKeyedUnarchiver.unarchiveObjectWithFile(fileURL.path!) as? [Int:GoogleItems] {
        println(loadedDic)
    }
}