Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new NSError in Swift (to reject a Promise from PromiseKit)

Tags:

I have been trying to use PromiseKit, and I'm stuck at rejecting a promise.

Promise rejection is done either by calling a reject function with an NSError as argument.

func getAPromise() -> Promise<Bool> {
    return Promise<Bool> { fulfiller, rejecter in
        let diceRoll = Int(arc4random_uniform(7))
        if diceRoll < 4 {
             // rejecter(?) how do I call this rejection correctly ?
        } else {
             fulfiller(true)
        }
}

Simply getting an instance of NSError would help me.

EDIT:

NSError("somedomain", 123, [])

complains with "Extra argument in call".

like image 988
AsTeR Avatar asked Dec 12 '14 12:12

AsTeR


People also ask

How do I reject a promise in Swift?

Promise rejection is done either by calling a reject function with an NSError as argument. Simply getting an instance of NSError would help me.

What is PromiseKit Swift?

PromiseKit is a Swift implementation of promises. While it's not the only one, it's one of the most popular. In addition to providing block-based structures for constructing promises, PromiseKit also includes wrappers for many of the common iOS SDK classes and easy error handling.


1 Answers

You have two problems in this code:

NSError("somedomain", 123, [])
  • All initialization parameters of NSError have external name.
  • Empty Dictionary literal is [:], not []. [] is for Array

Try:

NSError(domain: "somedomain", code: 123, userInfo: [:])

Or, if you don't have any userInfo, you might want to pass nil for it.

NSError(domain: "somedomain", code: 123, userInfo: nil)
like image 107
rintaro Avatar answered Oct 18 '22 16:10

rintaro