Ios 错误:可选绑定的初始值设定项必须具有可选类型,而不是';DetailViewController';
我正在尝试将信息从我的DetailViewController传递回我的MasterViewController,其中包含一个TableView,但是我的代码中出现了一个错误,我不确定问题可能是什么Ios 错误:可选绑定的初始值设定项必须具有可选类型,而不是';DetailViewController';,ios,swift,Ios,Swift,我正在尝试将信息从我的DetailViewController传递回我的MasterViewController,其中包含一个TableView,但是我的代码中出现了一个错误,我不确定问题可能是什么 @IBAction func saveDetails(sender: UIStoryboardSegue) { print("saveDetails triggered") if let sourceViewController = sender.sourceViewCont
@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)
}
}
}
if let
构造需要是可选的,因此它可能会失败,并因此采用else
分支:
if let sourceViewController = sender.sourceViewController as? DetailViewController, ...
注意
作为?
而不是作为
如果let构造需要是可选的,那么它可能会失败,从而执行else
分支:
if let sourceViewController = sender.sourceViewController as? DetailViewController, ...
注意
作为?
而不是作为代码>我更改了它,它将错误更改为“错误:可选绑定的初始值设定项必须具有可选类型,而不是“Person”。我修复了错误,必须转到并更改创建Person对象的位置。。。必须将其从var person=person(…)更改为var person=person?()我将其更改为“错误:可选绑定的初始值设定项必须具有可选类型,而不是'person'”。我修复了错误,必须转到创建person对象的位置。。。必须将其从var person=person(…)更改为var person=person?()