错误:可选绑定的初始化程序必须具有可选类型,而不是' DetailViewController'

时间:2016-04-13 10:45:22

标签: ios swift

我试图将信息从我的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)
        }
    }
    }

1 个答案:

答案 0 :(得分:2)

if let构造必须是可选的,因此它可能会失败,从而占用else分支:

if let sourceViewController = sender.sourceViewController as? DetailViewController, ...

注意as?而不是as!