简体   繁体   English

如何修复 escaping 关闭? 错误是:转换非转义值可能允许它转义

[英]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.

相关问题 将非转义值转换为 'Any' 可能允许它转义错误 - 在非转义函数内 - Converting non-escaping value to 'Any' may allow it to escape error - within not escaping function Xcode 10 Swift构建错误:“将非转义的值转换为'T'可能会使其逸出” - Xcode 10 Swift build error: “Converting non-escaping value to 'T' may allow it to escape” 非转义参数的关闭使用可能允许它转义 - Closure use of non-escaping parameter may allow it to escape Swift 3:关闭使用非转义参数可能允许它逃脱 - Swift 3 :Closure use of non-escaping parameter may allow it to escape 错误:将非转义参数'publicationQuery'分配给@escaping闭包 - Error: Assigning non-escaping parameter 'publicationQuery' to an @escaping closure 将非转义闭包传递给 function 期望 @escaping 闭包 - Passing non-escaping closure to function expecting an @escaping closure Swift-3:具有转义和非转义行为的闭包 - Swift-3: closure with escaping and non-escaping behaviour together 转义闭包捕获非转义参数“完成”(Swift 5) - Escaping closure captures non-escaping parameter 'completion' (Swift 5) 转义闭包捕获非转义参数“函数”Xcode 说 - Escaping closure captures non-escaping parameter 'function' Xcode says Swift:转义闭包捕获非转义参数“onCompletion” - Swift: Escaping closure captures non-escaping parameter 'onCompletion'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM