0

I'm trying to get a basic promise working with PromiseKit. However the following code wont compile:

import Foundation
import PromiseKit

class MyClass {
    var myInt: Int?

    func sample() -> Promise<AnyObject> {
        return Promise { fulfill, reject in
            fulfill(1)
        }.then { data -> Int in
            return 3
        }
    }

    init() {
        sample().then { data -> Void in
            debugPrint("got data: \(data)")
        }
    }
}

This is the error I get:

command failed due to signal: segmentation fault: 11

This is pretty frustrating. Has anyone encountered this?

rclark
  • 301
  • 3
  • 13
  • Is the error coming from the Swift compiler, or the program when you run it? – kennytm May 12 '16 at 06:06
  • Its coming from the swift compiler whenever I try to build @kennytm – rclark May 12 '16 at 06:08
  • If you go to the Report navigator in Xcode (press ⌘8, or click the on the left panel), you should be able to find the compilation log and find out which line it is causing the compiler to crash. – kennytm May 12 '16 at 06:10
  • Thanks @kennytm. It appears that if I change this line: `}.then { data -> Int in` to `.then { data -> NSNumber in` then it builds fine – rclark May 12 '16 at 06:14

1 Answers1

1

This is because Int is not AnyObject

func sample() -> Promise<AnyObject> {
    return Promise { fulfill, reject in
        fulfill(1)
    }.then { data -> Int in
        return 3
    }
}

This is most likely fixed in Swift 3, however either of these will fix the compile:

func sample() -> Promise<Int> {
    return Promise { fulfill, reject in
        fulfill(1)
    }.then { data -> Int in
        return 3
    }
}

Or:

func sample() -> Promise<AnyObject> {
    return Promise { fulfill, reject in
        fulfill(1)
    }.then { data -> NSNumber in
        return 3
    }
}
mxcl
  • 26,392
  • 12
  • 99
  • 98