我正在编写一个应用,试图通过Codable将数组中的数组保存到UserDefaults
中,但是它崩溃了,并出现以下错误:
由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[Here.UserEntries encodeWithCoder:]:无法识别的选择器已发送到实例 0x600000649e70'
这是我的保存方式:
class UserEntries: NSObject, Codable {
struct Question : Codable {
var question: String
var answer: String
enum CodingKeys: String, CodingKey {
case question
case answer
}
init(question: String, answer: String) {
self.question = question
self.answer = answer
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(question, forKey: .question)
try container.encode(answer, forKey: .answer)
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
question = try container.decode(String.self, forKey: .question)
answer = try container.decode(String.self, forKey: .answer)
}
}
var date: String
var questions: [Question]
enum CodingKeys: String, CodingKey {
case date
case questions
}
init(date: String, questions: [Question]) {
self.date = date
self.questions = questions
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(date, forKey: .date)
try container.encode(questions, forKey: .questions)
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
date = try container.decode(String.self, forKey: .date)
questions = [try container.decode(Question.self, forKey: .questions)]
}
}
其中userEntry是:
let userEntry = UserEntries(date: String(todayDate), questions: [UserEntries.Question(question: q1Text, answer: q1Answer), UserEntries.Question(question: q2Text, answer: q2Answer)])
然后
UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: userEntry), forKey: "allEntries")
到底出了什么问题?我有种无法将数组保存在数组中的感觉,但是该如何解决?
答案 0 :(得分:1)
此行:
questions = [try container.decode(Question.self, forKey: .questions)]
不正确。要获取Question
的数组,您应该解码数组[Question].self
的类型:
questions = try container.decode([Question].self, forKey: .questions)
此外,请注意Decodable
/ Encodable
与JSONDecoder
/ JSONEncoder
一起使用,请参见official docs。据此,这是您需要保存的内容(编码为数据然后保存):
let jsonEncoder = JSONEncoder()
if let value = try? jsonEncoder.encode(userEntry) {
UserDefaults.standard.set(value, forKey: "allEntries")
}
相反,获取数据,如果是,则尝试解码对象:
let jsonDecoder = JSONDecoder()
if let data = UserDefaults.standard.data(forKey: "allEntries"),
let userEntry = try? jsonDecoder.decode(UserEntries.self, from: data) {
// here you get userEntry
}
示例:
let userEntry = UserEntries(date: "1 Sep 18",
questions: [
UserEntries.Question(question: "q1Text", answer: "q1Answer"),
UserEntries.Question(question: "q2Text", answer: "q2Answer")
])
let jsonEncoder = JSONEncoder()
if let value = try? jsonEncoder.encode(userEntry) {
UserDefaults.standard.set(value, forKey: "allEntries")
}
let jsonDecoder = JSONDecoder()
if let data = UserDefaults.standard.data(forKey: "allEntries"),
let userEntry = try? jsonDecoder.decode(UserEntries.self, from: data) {
print(userEntry.date) // 1 Sep 18
}