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

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

问题描述

我已经更新我的代码到Xcode 8.0 beta 6,但我被困住似乎是关于新的非转义闭包默认。在下面的代码中,Xcode建议在 completion:前添加 @escaping 但仍然不会编译和循环。 EDIT :实际上,Xcode建议在之后 完成后添加@escaping。该提醒仍可能显示,但清理和编译将删除它。)该代码如何重写/修正在更新的Swift 3中工作?
我看了新的手册,但我找不到正确的代码示例。

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

callSomeOtherFunc(withCompletion:completion)
}

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

Swift 3:closure参数属性现在应用于参数 type ,而不是参数本身



在Swift 3之前,closure属性 @autoclosure @noescape 用于属性的参数,但现在属于参数类型;请参阅以下接受的Swift进展计划:





您的特定问题涉及参数类型属性 @escaping (适用相同的新规则),如接受的Swift演进提议默认情况下允许闭包参数为非转义:





这些提案现在都在Xcode 8的测试阶段实施(请参见有关Xcode的发行说明8 beta 6 ;需要访问的开发人员帐户登录)


Xcode 8 beta 6 - Swift编译器:Swift语言



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



...



Swift和Apple LLVM编译器:Swift语言



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


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

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






/ p>


@CristiBăluţă:escape是什么?从来没有看到这个关键字
before swift3 auto-conversion ... 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天全站免登陆