Class declaration cannot close over value 'fulfill' defined in outer scope - Swift 2.0

3.6k Views Asked by At

I'm trying to convert my app from Swift 1.2 to Swift 2.0 and I'm encountering the following error:

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
            }
        }
    }
}

How can I make B (or the then closure) capture fulfill properly?

PS: Promise comes from PromiseKit, and I'm running Xcode 7 beta 1.

1

There are 1 best solutions below

0
On BEST ANSWER

You should be able to workaround this by assigning fulfill to a local that you capture instead.

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
            }
        }
    }
}