如何在 SWIFT 中将词典添加到 Plist
How to add Dictionary to Plist in SWIFT
我有一个如下类型的字典
var favoriteGooglelocations = [Int:GoogleItems]()
其中 GoogleItems 是 class。
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,还有其他方法吗?
您需要将字典转换为 NSDictionary
以便您可以使用 writeToFile
尝试用这个投射
let succeed = (favoriteGooglelocations as NSDictionary).writeToFile(path, atomically: true)
如果 succeed 为 false,请检查您的 GoogleItems class,它应该符合 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)
}
}
更新: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)
}
}
我有一个如下类型的字典
var favoriteGooglelocations = [Int:GoogleItems]()
其中 GoogleItems 是 class。
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,还有其他方法吗?
您需要将字典转换为 NSDictionary
以便您可以使用 writeToFile
尝试用这个投射
let succeed = (favoriteGooglelocations as NSDictionary).writeToFile(path, atomically: true)
如果 succeed 为 false,请检查您的 GoogleItems class,它应该符合 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)
}
}
更新: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)
}
}