У меня ошибка при попытке закодировать мой объект. Я нашел много ответов, но это не сработало для меня: (
Я сократил свой код, оставив только важные
Это основной класс "Город" с кортежем и массивамив нем "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)
}
}
И здесь я пытаюсь инициализировать массив "Город "с геттером и сеттером
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?