非转义参数的关闭使用可能允许它转义 [英] Closure use of non-escaping parameter may allow it to escape

查看:13
本文介绍了非转义参数的关闭使用可能允许它转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个协议:

enum DataFetchResult {
    case success(data: Data)
    case failure
}

protocol DataServiceType {
    func fetchData(location: String, completion: (DataFetchResult) -> (Void))
    func cachedData(location: String) -> Data?
}

使用示例实现:

    /// An implementation of DataServiceType protocol returning predefined results using arbitrary queue for asynchronyous mechanisms.
    /// Dedicated to be used in various tests (Unit Tests).
    class DataMockService: DataServiceType {

        var result      : DataFetchResult
        var async       : Bool = true
        var queue       : DispatchQueue = DispatchQueue.global(qos: .background)
        var cachedData  : Data? = nil

        init(result : DataFetchResult) {
            self.result = result
        }

        func cachedData(location: String) -> Data? {
            switch self.result {
            case .success(let data):
                return data
            default:
                return nil
            }
        }

        func fetchData(location: String, completion: (DataFetchResult) -> (Void)) {

            // Returning result on arbitrary queue should be tested,
            // so we can check if client can work with any (even worse) implementation:

            if async == true {
                queue.async { [weak self ] in
                    guard let weakSelf = self else { return }

                    // This line produces compiler error: 
                    // "Closure use of non-escaping parameter 'completion' may allow it to escape"
                    completion(weakSelf.result)
                }
            } else {
               completion(self.result)
            }
        }
    }

上面的代码在 Swift3 (Xcode8-beta5) 中编译和运行,但不再适用于 beta 6.你能指出根本原因吗?

The code above compiled and worked in Swift3 (Xcode8-beta5) but does not work with beta 6 anymore. Can you point me to the underlying cause?

推荐答案

这是由于函数类型参数的默认行为发生了变化.在 Swift 3 之前(特别是 Xcode 8 beta 6 附带的构建),它们将默认为转义 - 您必须标记它们 @noescape 以防止它们被存储或捕获,这保证它们不会超过函数调用的持续时间.

This is due to a change in the default behaviour for parameters of function type. Prior to Swift 3 (specifically the build that ships with Xcode 8 beta 6), they would default to being escaping – you would have to mark them @noescape in order to prevent them from being stored or captured, which guarantees they won't outlive the duration of the function call.

然而,现在 @noescape 是函数类型参数的默认值.如果要存储或捕获此类函数,现在需要将它们标记为 @escaping:

However, now @noescape is the default for function-typed parameters. If you want to store or capture such functions, you now need to mark them @escaping:

protocol DataServiceType {
  func fetchData(location: String, completion: @escaping (DataFetchResult) -> Void)
  func cachedData(location: String) -> Data?
}

func fetchData(location: String, completion: @escaping (DataFetchResult) -> Void) {
  // ...
}

请参阅 Swift Evolution 提案 有关此更改的更多信息.

See the Swift Evolution proposal for more info about this change.

这篇关于非转义参数的关闭使用可能允许它转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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