Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 219
Barger27 Avatar asked Oct 31 '22 20:10

Barger27


1 Answers

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.

like image 194
Jasper Blues Avatar answered Nov 15 '22 10:11

Jasper Blues