Swift: 将一个对象数组和另一个对象数组保存到 NSUserDefaults 中
Swift: Save an Array of Objects with another Array of Objects inside into NSUserDefaults
我一直在努力完成我的第一个应用程序,但我正处于需要保存用户数据但无法保存的地步。我有一个对象数组,里面还有一个 "Questions" 对象数组:
struct Question {
let question: String
let answer: String
}
struct UserEntry {
let date: String
let questions: [Question]
}
let userEntry = [UserEntries(date: todayDate, questions: [Question(answer: mood), Question(question: q1Text, answer: q1Answer), Question(question: q2Text, answer: q2Answer)])]
但是,这会引发错误:无法将类型 'ThisViewController.Question' 的值转换为预期的元素类型 'UserEntries.Question'。在另一个堆栈答案之后也为其创建了一个 class:
class UserEntries: NSObject, NSCoding {
struct Question {
var question: String
var answer: String
}
var date: String
var questions: [Question]
init(date: String, questions: [Question]) {
self.date = date
self.questions = questions
}
required convenience init(coder aDecoder: NSCoder) {
let date = aDecoder.decodeObject(forKey: "date") as! String
let question = aDecoder.decodeObject(forKey: "questions")
self.init(date: date, questions: question as! [UserEntries.Question])
}
func encode(with aCoder: NSCoder) {
aCoder.encode(date, forKey: "date")
aCoder.encode(questions, forKey: "questions")
}
}
我是这样保存数据的:
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: userEntry)
UserDefaults.standard.set(encodedData, forKey: "allEntries")
我很困惑哪里出了问题,非常感谢任何帮助或指导!
您的问题是您在两个地方创建了 Question
对象。
看看报错信息:
Cannot convert value of type 'ThisViewController.Question' to expected element type 'UserEntries.Question'.
现在看你提供的代码,首先我猜是来自YourViewController
:
struct Question {
let question: String
let answer: String
}
struct UserEntry {
let date: String
let questions: [Question]
}
let userEntry = [UserEntries(date: todayDate, questions: [Question(answer: mood), Question(question: q1Text, answer: q1Answer), Question(question: q2Text, answer: q2Answer)])]
然后:
class UserEntries: NSObject, NSCoding {
struct Question {
var question: String
var answer: String
}
...
所以...您在 ThisViewController
中定义了 Question
,然后在 UserEntries
class 中定义了另一个 Question
。
编译器告诉您它认为您使用的是 ThisViewController.Question
,您不能将其添加到 UserEntries.Question
,因为对于编译器来说它们是两个不同的东西。
解决方案
当您将 Question
和 UserEntry
逻辑移动到符合 NSCoding
的 UserEntries
class 中时,您不再需要它在 YourViewController
中也是如此。
所以,从你的 YourViewController
中删除 Question
和 UserEntry
结构,这样你在 UserEntries
中只有一个 Question
并且事情应该(希望如此)再次工作。
希望对您有所帮助。
我一直在努力完成我的第一个应用程序,但我正处于需要保存用户数据但无法保存的地步。我有一个对象数组,里面还有一个 "Questions" 对象数组:
struct Question {
let question: String
let answer: String
}
struct UserEntry {
let date: String
let questions: [Question]
}
let userEntry = [UserEntries(date: todayDate, questions: [Question(answer: mood), Question(question: q1Text, answer: q1Answer), Question(question: q2Text, answer: q2Answer)])]
但是,这会引发错误:无法将类型 'ThisViewController.Question' 的值转换为预期的元素类型 'UserEntries.Question'。在另一个堆栈答案之后也为其创建了一个 class:
class UserEntries: NSObject, NSCoding {
struct Question {
var question: String
var answer: String
}
var date: String
var questions: [Question]
init(date: String, questions: [Question]) {
self.date = date
self.questions = questions
}
required convenience init(coder aDecoder: NSCoder) {
let date = aDecoder.decodeObject(forKey: "date") as! String
let question = aDecoder.decodeObject(forKey: "questions")
self.init(date: date, questions: question as! [UserEntries.Question])
}
func encode(with aCoder: NSCoder) {
aCoder.encode(date, forKey: "date")
aCoder.encode(questions, forKey: "questions")
}
}
我是这样保存数据的:
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: userEntry)
UserDefaults.standard.set(encodedData, forKey: "allEntries")
我很困惑哪里出了问题,非常感谢任何帮助或指导!
您的问题是您在两个地方创建了 Question
对象。
看看报错信息:
Cannot convert value of type 'ThisViewController.Question' to expected element type 'UserEntries.Question'.
现在看你提供的代码,首先我猜是来自YourViewController
:
struct Question {
let question: String
let answer: String
}
struct UserEntry {
let date: String
let questions: [Question]
}
let userEntry = [UserEntries(date: todayDate, questions: [Question(answer: mood), Question(question: q1Text, answer: q1Answer), Question(question: q2Text, answer: q2Answer)])]
然后:
class UserEntries: NSObject, NSCoding {
struct Question {
var question: String
var answer: String
}
...
所以...您在 ThisViewController
中定义了 Question
,然后在 UserEntries
class 中定义了另一个 Question
。
编译器告诉您它认为您使用的是 ThisViewController.Question
,您不能将其添加到 UserEntries.Question
,因为对于编译器来说它们是两个不同的东西。
解决方案
当您将 Question
和 UserEntry
逻辑移动到符合 NSCoding
的 UserEntries
class 中时,您不再需要它在 YourViewController
中也是如此。
所以,从你的 YourViewController
中删除 Question
和 UserEntry
结构,这样你在 UserEntries
中只有一个 Question
并且事情应该(希望如此)再次工作。
希望对您有所帮助。