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

前端之家收集整理的这篇文章主要介绍了闭包 – 使用非转义参数可以允许它逃逸前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个协议:
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

原文链接:https://www.f2er.com/swift/321276.html

猜你在找的Swift相关文章