Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
Swift 不推荐使用未标记尾随闭包的反向匹配。。。有没有办法让它安静下来,而不是停止使用尾随闭包?_Swift - Fatal编程技术网

Swift 不推荐使用未标记尾随闭包的反向匹配。。。有没有办法让它安静下来,而不是停止使用尾随闭包?

Swift 不推荐使用未标记尾随闭包的反向匹配。。。有没有办法让它安静下来,而不是停止使用尾随闭包?,swift,Swift,Swift编译器今天的新功能让我大吃一惊。。警告: Backward matching of the unlabeled trailing closure is deprecated; label the argument with 'onSuccess' to suppress this warning 我几乎在任何地方都使用尾随闭包语法,我不知道这个案例的问题在哪里 功能定义如下: func send<Data>(operation: CSOperation<Data&g

Swift编译器今天的新功能让我大吃一惊。。警告:

Backward matching of the unlabeled trailing closure is deprecated; label the argument with 'onSuccess' to suppress this warning
我几乎在任何地方都使用尾随闭包语法,我不知道这个案例的问题在哪里

功能定义如下:

func send<Data>(operation: CSOperation<Data>, title: String, progress: Bool = true,
        canCancel: Bool = true, failedDialog: Bool = true, onInternetFailed: (() -> Void)? = nil,
        onSuccess: ((Data) -> Void)? = nil) -> CSProcess<Data> {}
编译器希望我这样写:

send(operation: model.server.downloadCensusDocument(), title: .page_thanks_confirmation_download) {
        documentController = UIDocumentInteractionController(url: $0.url)
        ...
    }
    send(operation: model.server.downloadCensusDocument(), title: .page_thanks_confirmation_download, onSuccess: {
        documentController = UIDocumentInteractionController(url: $0.url)
        ...
    })

有没有办法让它静音,而不是停止使用尾随闭包?

正如您可能知道的,关键的新特性是,
onInternetFailed:
函数和
onSuccess:
参数现在都可以作为尾随闭包。多个尾随闭包。规则是,当您这样做时,第一个不被标记,其他的被标记,没有逗号。因此:

send(....) {
    // onInternetFailed function body
} onSuccess: {
    // onSuccess function body
}
好的,但这里的问题是:有两个可选的尾随闭包,所以如果省略一个,那么省略哪一个

您假设忽略了第一个。编译器理解您的想法,并且它允许这种省略,但是有一个警告,暂时,因为最终它将成为非法的-或者,更严格地说,最终将假定省略的是第二个

因此,为了避免警告,必须同时包含两个闭包

所以,我现在要写

send(operation: model.server.downloadCensusDocument(),
    title: .page_thanks_confirmation_download) 
        {} onSuccess: {
             documentController = UIDocumentInteractionController(url: $0.url)
            //...
        }

这就消除了警告,我认为这看起来并不太糟糕。两个闭包都是尾随的,而第一个闭包只是空的。

有关详细信息,请参见我的。即使是编辑器也会自动知道它是最后一个闭包,并会为我自动修复它,因此确保编译器也会知道,并在没有警告的情况下自动选择相同的闭包,但我知道我问错了地方:)不,请阅读此处和文章中的内容(以及在官方语言提案中的方式)它现在正在减少你的松弛,所以它只是警告你。但是在未来,它将是第一个,如果你没有改变它,你的代码就会被破解。另外两个选项:1。使<代码>成功> <代码>不是一个可选的。这也解决了歧义。2。你也可以考虑用一个完整的完成句柄关闭重构,<代码>(结果)。->Void,这使得
数据
将在
上返回更加清晰。success
Error
将在
上返回。failure
使合同更加清晰。有趣的建议@Rob我将查看一下。。。