类声明不能关闭外部作用域中定义的值'fulfill' - Swift 2.0

时间:2015-06-14 04:11:23

标签: xcode swift closures promise

我正在尝试将我的应用程序从Swift 1.2转换为Swift 2.0,我遇到以下错误:

class B {
    func test() -> Promise<A> {
        return Promise<A> { (fulfill, reject) -> Void in
            anotherPromise.then { _ -> Void in
                return fulfill(A()) // Class declaration cannot close over value 'fulfill' defined in outer scope
            }
        }
    }
}

如何让B(或then关闭)正确捕获fulfill

PS:Promise来自PromiseKit,我正在运行Xcode 7 beta 1.

1 个答案:

答案 0 :(得分:3)

您应该可以通过将class B { func test() -> Promise<A> { return Promise<A> { (fulfill, reject) -> Void in let innerFulfill = fulfill // close on this instead anotherPromise.then { _ -> Void in return innerFulfill(A()) // Class declaration cannot close over value 'fulfill' defined in outer scope } } } } 分配给您捕获的本地来解决此问题。

 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);