21

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".

AsTeR
  • 7,247
  • 14
  • 60
  • 99
  • When all else fails [read the instructions](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSError_Class/). – Hot Licks Dec 12 '14 at 13:03
  • Oh thanks really helpful and productive! Thanks for the down vote too... Any answer instead of pointing to the doc I just read? In case you are wondering. Yes, calling the constructor of NSError doesn't work, e.g.: `NSError("somedomain", 123, [])`. – AsTeR Dec 12 '14 at 13:06
  • But you never said that. You described no error (still haven't). – Hot Licks Dec 12 '14 at 13:08
  • Neither said I the contrary. I asked how to instantiate an NSError in Swift, because so far I found no way to do it, and so far you are not providing an answer (so I guess you have never done it) so I guess you too, which lead me again to: "Why the down vote?" – AsTeR Dec 12 '14 at 13:46
  • Well, for one, because you didn't show the failing code or the error. – Hot Licks Dec 12 '14 at 13:56
  • Ok you have my opinion, I have mine. I was expecting more openness there, maybe I'm wrong, enjoy your day. – AsTeR Dec 12 '14 at 13:59
  • So, tell me -- would you have gotten an answer without posting the failing statement and the error message? – Hot Licks Dec 12 '14 at 17:00
  • Indeed not. So tell me, if you are trying to build a reliable professional community do you think that bashing bad practices instead of mentoring is a nice way to foster openness and engagement? – AsTeR Dec 14 '14 at 12:32
  • Was your first response fostering "openness and engagement"? You came in like a spoiled brat. Didn't think of your readers (who are not being paid to be your servants) and create a *complete* question which at the very least included the failing code and the error message. Grow up!! – Hot Licks Dec 14 '14 at 14:07
  • (And you've got a rep, so you should know what a proper question looks like.) – Hot Licks Dec 14 '14 at 14:07
  • You are right about my question, which doesn't prevent me from being right about comment. – AsTeR Dec 14 '14 at 15:12

1 Answers1

52

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)
rintaro
  • 51,423
  • 14
  • 131
  • 139