我正在摆弄桌面视图,我遇到了错误无法指定类型的值'答案'值为' [答案]'
答案是一个具有以下定义的类:
class Answer {
var EnglishAnswer: String = ""
var ChineseAnswer: String = ""
init(newEng: String, newChi: String){
self.EnglishAnswer = newEng
self.ChineseAnswer = newChi
}
}
我在prepareforsegue方法中遇到了问题:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexpath: NSIndexPath = self.tableView.indexPathForSelectedRow!
var DestinationViewController = segue.destinationViewController as! AnswerTableViewController
var ArrayofArrayswithAnswerElementsTwo : Answer
var ArrayofArrayswithAnswerElements = levelsArray.map( { $0.AnswersArray })
ArrayofArrayswithAnswerElementsTwo = ArrayofArrayswithAnswerElements[indexpath.row]
DestinationViewController.ArrayofArrayswithAnswerElements = ArrayofArrayswithAnswerElementsTwo
}
答案 0 :(得分:1)
明确错误地说明您尝试将Answer
个对象数组分配给声明仅保留Answer
个对象的变量。
此变量用于保存Answer
对象:
var ArrayofArrayswithAnswerElementsTwo : Answer
我怀疑这一行正在重新调整Answer
个对象的数组:
ArrayofArrayswithAnswerElementsTwo = ArrayofArrayswithAnswerElements[indexpath.row]
您需要在prepareForSegue
功能中设置断点,检查模型,打印对象并检查正确的分配。
解决方案:您的问题可能有两种解决方案:
首先,像这样定义ArrayofArrayswithAnswerElementsTwo
var ArrayofArrayswithAnswerElementsTwo : [Answer]
其次,让ArrayofArrayswithAnswerElements[indexpath.row]
返回Answer
而不是[Answer]
PS:变量的命名约定与行业惯例不同步。变量名称应以小写字母开头,并且应尽可能短。