闭包 – 使用非转义参数可以允许它逃逸

我有一个协议:
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了。你能指点我的根本原因吗?

这是由于函数参数的认行为的改变。在Swift 3(特别是Xcode 8 beta 6附带的版本)之前,他们认使用转义 – 你必须将它们标记为@noescape,以防止它们被存储或捕获,因此保证它们不会后调用函数退出

然而,现在@noescape是认的 – 现在你必须将函数参数标记为@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 proposal

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...