objective-cswiftswift2xcode7xcode7-beta3

Swift 2.0 interop with Objective C not working as expected?


I'm porting a Swift 1.2 framework to 2.0. After fixing the 3 million compiler errors courtesy of the new Swift 2.0 error handling scheme, I was finally able to link my test app (written in Objective C) to use the updated framework.

Xcode version 7.0 beta 3 (7A121l)

However, I ran into a problem. Some Swift functions are no longer generated into the automatically generated Objective C header (MyFramework-Swift.h) used by the Objective C test app.

Here is an example of a function that is not exposed: (My actual framework function returned an enum, but I tried to simplify to illustrate the problem more clearly).

public func calculateImportantValueDangerously() throws -> Int  
{  
    return 42  
}  

Note that other functions like the following actually do get exposed as expected (and can be called):

public func doSomething()  
{  
}  

public func doSomethingDangerous() throws  
{  
}  

public func calculateMeaninglessValue() -> Int  
{  
    return -1  
}  

Here's the Objective C side:

MyClass *newInstance = [[MyClass alloc] init];  
[newInstance doSomething];  

NSError *error = nil;  
[newInstance doSomethingDangerousAndReturnError:&error];  

long meaninglessValue = [newInstance calculateMeaninglessValue];  
NSLog(@"%ld", meaninglessValue);  

long importantValue = [newInstance calculateImportantValueDangerouslyAndReturnError:&error]; <-COMPILE ERROR
NSLog(@"%ld", importantValue);  

From watching this video, I had expected that it should "just work":

https://developer.apple.com/videos/wwdc/2015/?id=401

...but it seems like we can't currently use functions that both throw and return a value.

Is this a bug, or not implemented feature? My apologies if I missed something in the release notes somewhere.

Any advice appreciated.


Solution

  • It is not possible.

    If you annotate your method with @objc you will see the problem.

    Throwing method cannot be marked @objc because it returns a value of type 'Int'; return 'Void' or a type that bridges to an Objective-C class

    You can return only objects, primitives are not supported.