如何向可选闭包添加@noescape注释 [英] How to add @noescape annotation to optional closure
问题描述
我的函数有这个签名:
func foo(bar:String,baz:((String) - > ))?= nil)
现在我想让不必要的转义 self
在给定的闭包。
但是当我尝试这样:
func foo(bar:String,@noescape baz: >())?= nil)
编译器会提示:
@noescape只能应用于函数类型的参数
是否可以在可选参数中使用它?
解决方案
需求
如果您的要求如下:
-
baz
param is a closure
-
baz
param标有 @noescape
(因为您要在闭包代码中省略 self
)
- 在调用
foo
$ b期间可以省略 baz
$ b 解决方案
然后您可以使用以下语法:
func foo(bar:String,@noescape baz:((String) - >())= {_ in}){
}
您可以看到与您的代码的主要区别是:
- 这里
baz
不是可选类型
(但它是一个可选参数 / li>
- ,其默认值为
空闭包
而不是 nil
/ li>
示例
根据您的要求,无需使用 self
class Boo {let world =world
func boo(){
foo(hello){(something) - > ()in
print(world)
}
}
}
您也可以省略 baz
param
class boo {
let world =world
func boo(){
foo(hello)
}
}
更新:使用返回类型不同于Void的闭包
下面的用户 TadeasKriz
询问如何使用这种方法与一个闭包返回值不同 Void
。
这里是解决方案
func foo(bar:String,@noescape baz:((String) - >(Int))= {_ in return 0}){
}
这里 baz
param需要一个类型为 String
以及 Int
类型的返回值。
正如你所看到的,我添加了一个默认值给param,一个闭包返回 0
。请注意,默认的关闭不会被使用,所以你可以用任何 Int
值替换 0
p>
现在您可以决定是否将您的关闭传递给 baz
param
class Boo {
let world =world
func boo(){
foo(hello){ - > Int in
print(world)
return 100
}
}
}
或者,您可以完全省略 baz
param。
class Boo {
let world =world
func boo(){
foo(hello)
}
}
My function has this signature:
func foo(bar: String, baz: ((String) -> ())? = nil)
And now I want to make unecessary to escape self
inside the given closure.
But when I try this:
func foo(bar: String, @noescape baz: ((String) -> ())? = nil)
The compiler complains:
@noescape may only be applied to parameters of function type
Is it possible to use it in optional parameters?
Requirements
If your requirements are the following:
- the
baz
param is aclosure
- the
baz
param is marked with@noescape
(because you want to omitself
in the closure code) - the
baz
param can be omitted during the invocation offoo
Solution
Then you can use the following syntax
func foo(bar: String, @noescape baz: ((String) -> ()) = { _ in } ) {
}
As you can see the main difference from your code is that:
- here
baz
is not anoptional type
(but it's an "optional parameter") - and its default value is an
empty closure
not anil
value.
Examples
As you requested you can now pass a closure to baz
without the need of using self
class Boo {
let world = "world"
func boo() {
foo("hello") { (something) -> () in
print(world)
}
}
}
And you can also omit the baz
param
class Boo {
let world = "world"
func boo() {
foo("hello")
}
}
Update: using a closure with return type different from Void
In a comment below users TadeasKriz
asked about how to use this approach with a closure having the return value different the Void
.
Here it is the solution
func foo(bar: String, @noescape baz: ((String) -> (Int)) = { _ in return 0 } ) {
}
Here the baz
param does required a closure with 1 param of type String
and a return value of type Int
.
As you can see I added a default value to the param, a closure that does return 0
. Please note that the default closure will never be used so you can replace 0
with any Int
value you want.
Now you can decide whether to use pass your closure to the baz
param
class Boo {
let world = "world"
func boo() {
foo("hello") { (something) -> Int in
print(world)
return 100
}
}
}
Or, again, you can totally omit the baz
param.
class Boo {
let world = "world"
func boo() {
foo("hello")
}
}
这篇关于如何向可选闭包添加@noescape注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!