5
votes

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?

1
Some more information on why Typhoon requires @objc protocols would probably be relevant here. I don't think throws makes sense for @objc protocols... - nhgrif
Typhoon and @objc limitation in protocols The answer here explains why Typhoon requires it (from one of the contributors). It seems like if I could just format the method signature in an objective-c friendly way, the link that I referred to should work. But I haven't found the right format yet. - Barger27

1 Answers

2
votes

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.