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

标签 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)

Error : > [Int : GoogleItems] does not have a member named writeToFile

更新: 我写的路径是

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

 let documentDirectory = paths[0] as! String

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

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

最佳答案

您需要将 Dictionary 转换为 NSDictionary 以便您可以使用 writeToFile

尝试用这个转换

let succeed = (favoriteGooglelocations as NSDictionary).writeToFile(path, atomically: true)

如果 succeed 为 false,请检查您的 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)
        }

    }

关于ios - 如何在 SWIFT 中将词典添加到 Plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30659409/

相关文章:

ios - 无法让 Jenkins 与 faSTLane 一起工作

swift - 如何从字符串中过滤非数字

xcode - 音频在 YouTube 播放器助手应用程序中不起作用

iphone - 无法打开手工编码的plist文件

ios - 是否可以在消息 NSLocationWhenInUseUsageDescription 中添加笑脸?

ios - 从底部覆盖控制中心屏幕边缘平移?

ios - Facebook 帐户套件 用于测试的手机号码

ios - 如何使用 SQLite.swift 删除表或更新列?

ios - 安装 firebase 后生成推送通知

ios - 用于显示来自外部 URL 的多个图像的 TableView 模式