将闭包更新为 Swift 3 - @escaping [英] Updating closures to Swift 3 - @escaping
问题描述
我已将我的代码更新到 Xcode 8.0 beta 6,但我被困在似乎是关于新的非转义闭包默认值的问题上.在下面的代码中,Xcode 建议在下面代码的第一行中的 completion:
前面添加 @escaping
,但这仍然不会编译并循环.*
I've updated my code to Xcode 8.0 beta 6 but I got stuck with what seems to be about the new non escaping closure default. In the following code Xcode suggests to add @escaping
in front of completion:
in the first line of the below code, but that still won't compile and goes in circles. *
(EDIT:实际上,@escaping 应该添加在 after completion:
中,正如 Xcode 建议的那样.警报可能仍然显示,但清理和编译将删除它.)* 应如何重写/修复此代码以在更新的 Swift 3 中工作?我查看了新手册,但找不到合适的代码示例.
(EDIT: In fact, @escaping should be added in after completion:
, as Xcode suggests. The alert may still show but cleaning and compiling will remove it.)* How should this code be re-written / fixed to work in the updated Swift 3?
I've had a look in the new manual but I couldn't find proper code samples.
func doSomething(withParameter parameter: Int, completion: () -> ()) {
// Does something
callSomeOtherFunc(withCompletion: completion)
}
// Calling the method and execute closure
doSomething(withParameter: 2) {
// do things in closure
}
非常感谢任何帮助!
推荐答案
Swift 3:闭包参数属性现在应用于参数type,而不是参数本身
在 Swift 3 之前,闭包属性 @autoclosure
和 @noescape
曾经是闭包的属性参数,但现在是属性到参数type;请参阅以下已接受的 Swift 进化提案:
Swift 3: closure parameter attributes are now applied to the parameter type, and not the parameter itself
Prior to Swift 3, the closure attributes @autoclosure
and @noescape
used to be attributes to the closure parameter, but are now attributes to the parameter type; see the following accepted Swift evolution proposal:
您的具体问题与参数类型属性 @escaping
(适用相同的新规则)有关,如已接受的 Swift 进化提案中所述,默认情况下让闭包参数不转义:
Your specific question pertain to parameter type attribute @escaping
(for which the same new rule applies), as described in the accepted Swift evolution proposal to let closure parameters be non-escaping by default:
这些提议现在都在 Xcode 8 的 beta 阶段实施(请参阅 Xcode 8 beta 6 的发行说明; 访问需要开发帐户登录)
These proposals are now both implemented in the beta stage of Xcode 8 (see release notes for Xcode 8 beta 6; dev. account login needed for access)
Xcode 8 beta 6 的新功能 - Swift 编译器:Swift 语言
闭包参数默认是非转义的,而不是明确的使用 @noescape
进行注释.使用 @escaping
表示一个关闭参数可能会转义.@autoclosure(escaping)
现在写成@autoclosure @escaping
.注释 @noescape
和@autoclosure(escaping)
已弃用.(SE-0103)
Closure parameters are non-escaping by default, rather than explicitly
being annotated with @noescape
. Use @escaping
to indicate that a
closure parameter may escape. @autoclosure(escaping)
is now written as
@autoclosure @escaping
. The annotations @noescape
and
@autoclosure(escaping)
are deprecated. (SE-0103)
...
Xcode 8 beta 中的新功能 – Swift 和 Apple LLVM 编译器:Swift 语言
@noescape
和 @autoclosure
属性现在必须写入在参数类型之前而不是在参数名称之前.[SE-0049]
The @noescape
and @autoclosure
attributes must now be written
before the parameter type instead of before the parameter name. [SE-0049]
因此,您使用非默认的 @escaping
属性如下;应用于闭包参数的类型,而不是参数本身
Hence, you use the non-default @escaping
attribute as follows; applied to the type of the closure parameter, rather than the parameter itself
func doSomething(withParameter parameter: Int, completion: @escaping () -> ()) {
// ...
}
<小时>
(包括我在下面一个点赞评论中对问题的回答,因为评论不是关于 SO 的持久数据)
@Cristi Băluță:逃避有什么作用?从未见过这个关键词在 swift3 自动转换之前......
@Cristi Băluță: "What does escaping do? Never seen this keywords before swift3 auto-conversion ... "
参见例如上述SE-0103进化提案的链接(以及 beta 6 发行说明中引用的文本):以前,闭包参数默认情况下会转义(因此不需要存在用于转义的显式注释),但现在默认情况下是非转义的.因此,添加 @escaping
以明确注释闭包参数可能会转义(与其默认行为相反).这也解释了为什么 @noescape
现在已被弃用(无需注释默认行为).
See e.g. the link to the SE-0103 evolution proposal above (as well as the quoted text from the beta 6 release notes): previously, closure parameters were escaping by default (hence no need for the existence of an explicit annotation for escaping), but are now instead non-escaping, by default. Hence the addition of @escaping
to explicitly annotate that a closure parameter may escape (contrary to its default behaviour). This also explains why @noescape
is now deprecated (no need to annotate the default behaviour).
为了解释闭包参数转义的含义,我引用了 语言参考 - 属性:
For explaining what it means that a closure parameter is escaping, I quote the Language Reference - attributes:
"将此属性应用于方法或函数声明中的参数类型,以指示该参数的值可以存储为后来执行.这意味着允许该值比调用的生命周期."
这篇关于将闭包更新为 Swift 3 - @escaping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!