Search code examples
objective-cswiftinteropswift2typhoon

Swift 2 - @objc Protocol that throws an Error


I'm using Typhoon in a Swift project which requires protocols to be marked with @objc. I am attempting to upgrade my project to Swift 2.

In my iOS application, my service layer throws Errors back to the UI. However, despite my best efforts, I get a compile error:

Type 'ErrorThrower' does not conform to protocol 'Throwable'

@objc protocol Throwable {
    func doSomething(someParam:AnyObject) throws
}

@objc class ErrorThrower : NSObject, Throwable {
    func doSomething(someParam: AnyObject) throws {
        NSLog("An error is about to be thrown")
        throw GenericError.Generic
    }
}

enum GenericError : ErrorType {
    case Generic
}

I saw this post "Swift class does not conform to Objective-C protocol with error handling"

So, that made me try something like this:

@objc protocol Throwable {
    func doSomething(someParam:AnyObject) throws
}


class ErrorThrower : NSObject, Throwable {     
    @objc(doSomethingAndReturnError:someParam:)
    func doSomething(someParam: AnyObject) throws {
        NSLog("An error is about to be thrown")
        throw GenericError.Generic
    }
}

It doesn't complain about the @objc(...) on the implementation, but it still gives the same non-conforming protocol error.

I also tried this with no luck...

@objc protocol Throwable {
    func doSomethingAndReturnError(error:NSErrorPointer, someParam:AnyObject) -> Bool
}

What's the proper way in Swift 2 to declare a protocol with @objc and throw an error on methods?


Solution

  • Unfortunately, from what I researched today, I believe that Swift 2 style exception are incompatible with Objective-C, and therefore will not work with Typhoon.