无法在swift中分配值错误

时间:2015-10-04 02:59:43

标签: ios xcode swift2 xcode7

我正在摆弄桌面视图,我遇到了错误无法指定类型的值'答案'值为' [答案]'

答案是一个具有以下定义的类:

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

}

1 个答案:

答案 0 :(得分:1)

明确错误地说明您尝试将Answer个对象数组分配给声明仅保留Answer个对象的变量。

此变量用于保存Answer对象:

var ArrayofArrayswithAnswerElementsTwo : Answer

我怀疑这一行正在重新调整Answer个对象的数组:

ArrayofArrayswithAnswerElementsTwo = ArrayofArrayswithAnswerElements[indexpath.row]

您需要在prepareForSegue功能中设置断点,检查模型,打印对象并检查正确的分配。

解决方案:您的问题可能有两种解决方案:

首先,像这样定义ArrayofArrayswithAnswerElementsTwo

var ArrayofArrayswithAnswerElementsTwo : [Answer]

其次,ArrayofArrayswithAnswerElements[indexpath.row]返回Answer而不是[Answer]

PS:变量的命名约定与行业惯例不同步。变量名称应以小写字母开头,并且应尽可能短。