Ios Swift—在控制器之间传递CoreData时发生致命错误
这里我有两个表视图控制器,名为:Ios Swift—在控制器之间传递CoreData时发生致命错误,ios,swift,core-data,Ios,Swift,Core Data,这里我有两个表视图控制器,名为:customerTableViewController和customOrderListTableViewController。当单击customerTableViewController中的单元格时,我尝试将核心数据从customerTableViewController传递到customOrderListTableViewController。我通过故事板制作了一段“showCustomerOrderList” 但是目标控制器中的数据总是打印出来的 “致命错误:
customerTableViewController
和customOrderListTableViewController
。当单击customerTableViewController
中的单元格时,我尝试将核心数据从customerTableViewController
传递到customOrderListTableViewController
。我通过故事板制作了一段“showCustomerOrderList”
但是目标控制器中的数据总是打印出来的
“致命错误:在展开可选文件时意外发现零。”
价值观
当我使用传入的报复性实体属性时。我如何修复此问题?
代码如下:
//in customerTableViewController:
import CoreData
var customer: [Customer] = []
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showCustomerOrderList" {
let controller = segue.destination as! customOrderListTableViewController
controller.customer = customer[(tableView.indexPathForSelectedRow?.row)!]
}
//in customOrderListTableViewController:
import CoreData
var customer: Customer!
override func viewDidLoad() {
super.viewDidLoad()
print("\(self.customer.customerName)") //error here: Fatal error: Unexpectedly found nil while unwrapping an Optional value
}
在prepare方法中更正目标视图控制器的名称。请编写“customerOrderListTableViewController”而不是“customOrderListTableViewController”,很抱歉在编写过程中出现此类型的错误。其:customOrderListTableViewControllerCheck if
prepare(对于segue:sender:)调用
,并检查它是否在func viewDidLoad()之后
。如果是在之后,您将Segue从UITableViewCell
连接到customOrderListTableViewController
。相反,将其移除,将其从customerTableViewController
连接到customOrderListTableViewController
,并实现didSelectRowAtIndexPath并在那里执行检查。非常感谢感谢您的帮助!通过跟踪您的评论进行跟踪,现在就可以了!