[英]How to fix escaping closure? Error is: Converting non-escaping value may allow it to escape
Here is my code:这是我的代码:
class Main {
init() {
let x = Sub(s: foo)
}
func foo(completion: @escaping (String?)->Void) {
DispatchQueue.global().async {
completion(nil)
}
}
}
class Sub {
var s: ((String?)->Void)->Void
init(s: @escaping ((String?)->Void)->Void) {
self.s = s
}
}
I get error here let x = Sub(s: foo)
我在这里得到错误
let x = Sub(s: foo)
Converting non-escaping value to '(String?) -> Void' may allow it to escape`
将非转义值转换为 '(String?) -> Void' 可能允许它转义`
I have added all the escapes that XCode prompted me to add, but the error is still there.我已经添加了 XCode 提示我添加的所有转义,但错误仍然存在。 What do I need to do to fix this?
我需要做什么来解决这个问题?
You need another layer of @escaping
:您需要另一层
@escaping
:
class Sub {
var s: (@escaping (String?) -> Void) -> Void
init(s: @escaping (@escaping (String?) -> Void) -> Void) {
self.s = s
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.