更新关闭Swift 3 - @escaping [英] Updating closures to Swift 3 - @escaping

查看:141
本文介绍了更新关闭Swift 3 - @escaping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将我的代码更新为Xcode 8.0 beta 6,但是我似乎陷入了关于新的非转义关闭默认的似乎。在下面的代码中,Xcode建议在下面的代码的第一行添加完成之前的 @escaping 但是仍然不会编译并进入圈内。 *



编辑:事实上,@escaping应该在 :如Xcode所示,警报仍然可以显示,但是清理和编译将会删除它。)*该代码如何重新编写/修正以更新Swift 3中的工作?
我看过新手册,但找不到正确的代码示例。

  func doSomething (withParameter参数:Int,completion:() - >()){
//有东西

callSomeOtherFunc(withCompletion:completion)
}

//调用方法并执行关闭
doSomething(withParameter:2){
//在闭包
中执行的操作
解决方案

Swift 3:关闭参数属性现在应用于参数类型,而不是参数本身



在Swift 3之前,关闭属性 @autoclosure @noescape 曾经是关闭参数的属性,但现在属于参数;请参阅以下接受的Swift进化提案:





您的具体问题属于参数类型属性 @escaping (适用于相同的新规则),如接受的Swift Evolution提案中所述让闭包参数默认为不转义:





这些提案现在都在Xcode 8的beta阶段实现(请参阅 Xcode的发行说明8 beta 6 ; dev。帐户登录需要访问)


Xcode 8 beta 6中的新功能 - Swift编译器:Swift语言



默认情况下,关闭参数是不转义的,而不是明确地
@noescape 注释。使用 @escaping 表示
闭包参数可能会转义。 @autoclosure(escaping)现在写成
@autoclosure @escaping 。注释 @noescape
@autoclosure(escaping)已被弃用。 (SE-0103)



...



Swift和Apple LLVM编译器:Swift语言



@noescape @autoclosure 属性现在必须在参数类型之前写入
,而不是在参数名称之前。 [SE-0049]


因此,您使用非默认 @escaping 属性如下;应用于闭包参数的类型,而不是参数本身

  func doSomething(withParameter参数:Int,completion:@escaping() - >()){
// ...
}






(包括我在下面的upvoted评论中的一个问题的答案,因为注释不是SO上的持久性数据) / p>


@CristiBăluţă:逃避做什么?从未见过这个关键字
之前swift3自动转换... p>

请参见eg 上述SE-0103进化建议的链接(以及来自beta 6发行说明的引用文本):以前,默认情况下,闭包参数正在转义(因此不需要存在用于转义的显式注释),但是默认情况下现在是不转义的。因此,添加 @escaping 以显式地注释一个闭包参数可能会转义(与其默认行为相反)。这也解释了为什么现在不推荐使用 @noescape (不需要注释默认行为)。



为了解释闭包参数是否正在转义,我引用语言参考 - 属性


应用此属性在方法或函数声明中指定参数的类型,以指示参数的值可以存储在
以后执行,这意味着该值允许超过调用的
生命周期。



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: 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
}

Any help much appreciated!

解决方案

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:

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:

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)

New in Xcode 8 beta 6 - Swift Compiler: Swift Language

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)

...

New in Xcode 8 beta – Swift and Apple LLVM Compilers: Swift Language

The @noescape and @autoclosure attributes must now be written before the parameter type instead of before the parameter name. [SE-0049]

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 () -> ()) {
    // ...
}


(Including my answer to a question in an upvoted comment below, as comments are not persistent data on SO)

@Cristi Băluță: "What does escaping do? Never seen this keywords before swift3 auto-conversion ... "

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:

"Apply this attribute to a parameter’s type in a method or function declaration to indicate that the parameter’s value can be stored for later execution. This means that the value is allowed to outlive the lifetime of the call."

这篇关于更新关闭Swift 3 - @escaping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆