我正在构建一个测验应用,并希望让用户能够保存问题和答案组合。在我的应用程序中,我已经建立了一个结构来保存问题和答案。 用户可以浏览许多问题并保存您的愿望。
我希望保存问题并在另一个viewController中访问它。我想使用NSUserDefaults保存问题。但是,当我尝试将当前问题保存在屏幕上时,我收到错误消息,说明"无法转换类型'问题类型'期望参数类型' AnyObject'"。我该如何解决这个问题,以便保存问题并将其显示在另一个视图控制器中?
我正在使用Swift 2。
struct Question {
var Question : String!
var Answers : String!
}
var Questions = [Question]()
var QNumber = Int()
class circulatory: UIViewController {
@IBOutlet weak var labelForQuestion: UILabel!
@IBOutlet weak var textBoxForAnswer: UITextView!
override func viewDidLoad() {
//Load Questions
Questions = [
Question(Question: "This is Question 1", Answers: "This is answer 1"),
Question(Question: "This is Question 2", Answers: "This is answer 2"),
Question(Question: "This is Question 3", Answers: "This is answer 3"),
]
pickQuestion()
}
func pickQuestion() {
if Questions.count > 0 {
labelForQuestion.text = Questions[QNumber].Question
textBoxForAnswer.text = Questions[QNumber].Answer
}
}
@IBAction func save(sender: AnyObject) {
NSUserDefaults.standardUserDefaults().setObject(Question, forKey: "savedQuestion")
}