Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 错误:可选绑定的初始值设定项必须具有可选类型,而不是';DetailViewController';_Ios_Swift - Fatal编程技术网

Ios 错误:可选绑定的初始值设定项必须具有可选类型,而不是';DetailViewController';

Ios 错误:可选绑定的初始值设定项必须具有可选类型,而不是';DetailViewController';,ios,swift,Ios,Swift,我正在尝试将信息从我的DetailViewController传递回我的MasterViewController,其中包含一个TableView,但是我的代码中出现了一个错误,我不确定问题可能是什么 @IBAction func saveDetails(sender: UIStoryboardSegue) { print("saveDetails triggered") if let sourceViewController = sender.sourceViewCont

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

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?()