今天,Swift 编译器的新功能让我大吃一惊……警告:
Backward matching of the unlabeled trailing closure is deprecated; label the argument with 'onSuccess' to suppress this warning
Run Code Online (Sandbox Code Playgroud)
我几乎在所有地方都使用尾随闭包语法,但我不知道这种情况的问题出在哪里。
函数定义如下:
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> {}
Run Code Online (Sandbox Code Playgroud)
它是这样称呼的:
send(operation: model.server.downloadCensusDocument(), title: .page_thanks_confirmation_download) {
documentController = UIDocumentInteractionController(url: $0.url)
...
}
Run Code Online (Sandbox Code Playgroud)
编译器希望我这样写:
send(operation: model.server.downloadCensusDocument(), title: .page_thanks_confirmation_download, onSuccess: {
documentController = UIDocumentInteractionController(url: $0.url)
...
})
Run Code Online (Sandbox Code Playgroud)
有什么方法可以让它安静下来,而不是停止使用尾随闭包吗?
您可能知道,关键的新功能是函数onInternetFailed:
和onSuccess:
参数现在都允许作为尾随闭包。多个尾随闭包。规则是,当你这样做时,第一个没有标签,其他的没有标签会被标记,不带逗号。所以:
send(....) {\n // onInternetFailed function body\n} onSuccess: {\n // onSuccess function body\n}\n
Run Code Online (Sandbox Code Playgroud)\n好的,但是这里的问题是:有两个可选的尾随闭包,所以如果您省略其中一个,那么您会省略哪一个?
\n您假设您忽略了第一个。编译器理解你的想法,并且它允许这种省略,但暂时会发出警告,因为最终它将变得非法 \xe2\x80\x94 或者,更技术地说,最终会假设省略的一个是第二个个。
\n因此,为了避免出现警告,您必须包含两个闭包。
\n所以,我现在会写
\nsend(operation: model.server.downloadCensusDocument(),\n title: .page_thanks_confirmation_download) \n {} onSuccess: {\n documentController = UIDocumentInteractionController(url: $0.url)\n //...\n }\n
Run Code Online (Sandbox Code Playgroud)\n这消除了警告,我认为这看起来还不错。两个闭包都会跟踪,第一个闭包只是空的。
\n 归档时间: |
|
查看次数: |
2392 次 |
最近记录: |