尝试编码对象时出现错误。我找到了很多答案,但这对我不起作用:(
我削减了代码,只留下了重要的
这是主类“ City”,其中包含元组以及其中的“ cityHourlyForecast”和“ cityDailyForecast”数组
class City: NSObject, NSCoding {
var name: String
var currentWeather: (isDayTime: Bool, lastUpdate: Date?, temp_C: Int, temp_F: Int, weatherIcon: String, weatherText: String) =
( isDayTime: true,
lastUpdate: nil,
temp_C: 0,
temp_F: 0,
weatherIcon: "1",
weatherText: "" )
var hourlyForecast: Array<cityHourlyForecast> = []
var dailyForecast: Array<cityDailyForecast> = []
init(name: String) {
self.name = name
}
// MARK: NSCoding
func encode(with coder: NSCoder) {
coder.encode(self.name, forKey: "name")
coder.encode(self.currentWeather, forKey: "currentWeather")
coder.encode(self.hourlyForecast, forKey: "hourlyForecast")
coder.encode(self.dailyForecast, forKey: "dailyForecast")
}
required convenience init?(coder: NSCoder) {
guard let name = coder.decodeObject(forKey: "name") as? String
else { return nil }
self.init(name: name)
if let currentWeather = coder.decodeObject(forKey: "currentWeather") {
self.currentWeather = currentWeather as! (isDayTime: Bool, lastUpdate: Date?, temp_C: Int, temp_F: Int, weatherIcon: String, weatherText: String)
}
if let hourlyForecast = coder.decodeObject(forKey: "hourlyForecast") {
self.hourlyForecast = hourlyForecast as! Array<cityHourlyForecast>
}
if let dailyForecast = coder.decodeObject(forKey: "dailyForecast") {
self.dailyForecast = dailyForecast as! Array<cityDailyForecast>
}
}
}
cityHourlyForecast:
class cityHourlyForecast: NSObject, NSCoding {
var dateTime: String
var temp_C: Int
init (dateTime: String, temp_C: Int) {
self.dateTime = dateTime
self.temp_C = temp_C
}
func encode(with coder: NSCoder) {
coder.encode(dateTime, forKey: "dateTime")
coder.encode(temp_C, forKey: "temp_C")
}
required convenience init?(coder: NSCoder) {
guard let dateTime = coder.decodeObject(forKey: "dateTime") as? String,
let temp_C = coder.decodeObject(forKey: "temp_C") as? Int
else { return nil }
self.init(dateTime: dateTime, temp_C: temp_C)
}
}
cityDailyForecast:
class cityDailyForecast: NSObject, NSCoding {
var dateTime: String
var temp_min_C: Int
var temp_max_C: Int
init (dateTime: String, temp_min_C: Int, temp_max_C: Int) {
self.dateTime = dateTime
self.temp_min_C = temp_min_C
self.temp_max_C = temp_max_C
}
func encode(with coder: NSCoder) {
coder.encode(dateTime, forKey: "dateTime")
coder.encode(temp_min_C, forKey: "temp_min_C")
coder.encode(temp_max_C, forKey: "temp_max_C")
}
required convenience init?(coder: NSCoder) {
guard let dateTime = coder.decodeObject(forKey: "dateTime") as? String,
let temp_min_C = coder.decodeObject(forKey: "temp_min_C") as? Int,
let temp_max_C = coder.decodeObject(forKey: "temp_max_C") as? Int
else { return nil }
self.init(dateTime: dateTime, temp_min_C: temp_min_C, temp_max_C: temp_max_C)
}
}
在这里,我尝试使用getter和setter初始化“ City”的数组
var AccuCities:Array<City> {
get{
if let array = UserDefaults.standard.object(forKey: "Cities") as? Data {
let decodedArray = NSKeyedUnarchiver.unarchiveObject(with: array)
return decodedArray as! Array<City>
} else {
return []
}
}
set{
let encodedArray = NSKeyedArchiver.archivedData(withRootObject: newValue)
UserDefaults.standard.set(encodedArray, forKey: "Cities")
UserDefaults.standard.synchronize()
}
}
我该如何编码我的“城市”数组并将其保存到UserDefaults?