Ios Swift5数据在传递时为零,即使它不是';T
我在斯威夫特有个奇怪的问题 它与在视图控制器之间传递数据的其他问题有关,但是这里的区别在于,这是为了传递从“didSelectRowAt”方法加载的简单对象 选中行时,我正在从表视图控制器传递数据。数据作为对象传递,我可以打印数据并在议会中查看它,但当我尝试从中赋值时,我得到了一个可怕的错误:“在隐式展开可选值时意外发现零”。不知道我在这里错过了什么 从表视图控制器A:Ios Swift5数据在传递时为零,即使它不是';T,ios,swift,xcode,swift5.1,Ios,Swift,Xcode,Swift5.1,我在斯威夫特有个奇怪的问题 它与在视图控制器之间传递数据的其他问题有关,但是这里的区别在于,这是为了传递从“didSelectRowAt”方法加载的简单对象 选中行时,我正在从表视图控制器传递数据。数据作为对象传递,我可以打印数据并在议会中查看它,但当我尝试从中赋值时,我得到了一个可怕的错误:“在隐式展开可选值时意外发现零”。不知道我在这里错过了什么 从表视图控制器A: override func tableView(_ tableView: UITableView, didSelect
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "existingStudentSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! StudentDetailTableViewController
if let indexPath = tableView.indexPathForSelectedRow {
destinationVC.selectedStudent = studentsArray?[indexPath.row]
}
}
由表视图控制器B接收:
var selectedStudent: Students? {
didSet {
print("DATA RECEIVED: \(String(describing: selectedStudent))")
print("DATA TYPE: \ (type(of: selectedStudent))")
firstNameText.text = selectedStudent?.firstName
}
}
“print”语句显示我输入的测试数据,“type of”显示学生I类设置,如下所示
DATA RECEIVED: Optional(Students {created = 2019-09-24 12:07:23 +0000; name = Anna Altitude; ...(it's all here)
DATA TYPE: Optional<Students>
收到的数据:可选(学生{created=2019-09-2412:07:23+0000;name=Anna Altitude;…(全部在这里)
数据类型:可选
当我尝试在表视图控制器B的文本字段中显示文本时,代码失败。我在调用堆栈中看到,selectedStudent的数据为空。但这怎么可能?对象已填充且可访问。将此添加到
viewDidLoad
firstNameText.text = selectedStudent?.firstName
问题是当
didSet
触发firstNameText
时,由于vc尚未加载将其从didSet