Consider this Objective-C class:
@interface TestAPI : NSObject
-(nullable id)aNullableMethod;
-(nullable id)aNullableMethodWithError:(inout NSError **)error;
@end
I'm using TestAPI as an abstract base class and want to inherit from it in Swift.
Swift bridges this code to look like this (i.e. Counterparts menu -> Swift 4.2 Interface):
open class TestAPI : NSObject {
open func aNullableMethod() -> Any?
open func aNullableMethodWithError() throws -> Any
}
The problem is that aNullableMethodWithError() can return nil as a valid value.
How do I convince Swift that aNullableMethodWithError() returns Any? and not Any?
Or is there a better way to write the Objective-C method signature?
You may need to use
NS_SWIFT_NOTHROW:This is imported into Swift as:
If you want to import your method as
throws-function, you may need to change the method signature to follow the convention:trueor non-nil on no errorfalseor nil on errorSomething like this:
In Swift: