我试图将信息从我的DetailViewController传递回我的MasterViewController并且是一个TableView但是我的代码中出现错误并且不确定问题可能是什么?
@IBAction func saveDetails(sender: UIStoryboardSegue) {
print("saveDetails triggered")
if let sourceViewController = sender.sourceViewController as! DetailViewController, person = sourceViewController.person {
if let selectedIndexPath = tableView.indexPathForSelectedRow {
// Update an existing contact.
objects[selectedIndexPath.row] = person
tableView.reloadRowsAtIndexPaths([selectedIndexPath], withRowAnimation: .None)
} else {
// Add a new contact.
let newIndexPath = NSIndexPath(forRow: objects.count, inSection: 0)
objects.append(person)
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
}
}
}
答案 0 :(得分:2)
if let
构造必须是可选的,因此它可能会失败,从而占用else
分支:
if let sourceViewController = sender.sourceViewController as? DetailViewController, ...
注意as?
而不是as!
。