I’ve been having a bit of trouble with interop between hybrid Java/ObjC classes and Swift. A sample project demonstrating the problem can be found here.
In this example, I have a class ImageProvider which is declared in ObjC and implemented in Kotlin. Using this class in ObjC is not a problem - this can be verified by changing the view controller in the main storyboard to ObjCViewController. However, using an (ostensibly the same) view controller written in Swift (ViewController) causes a crash when instantiating the ImageProvider. Am I doing something wrong, or is there some sort of special case when using these kinds of classes from Swift?
If anyone has any suggestions I would be hugely appreciative. Thanks for your help!
-Damian
I tried that, and it doesn’t fix the problem. The problem appears when instantiating an instance of the class in Swift – not when doing so from Java or ObjC.
I’ve managed to work around the issue by adding a static factory method to the ObjC class which returns a new instance of the ImageProvider. Implementing this in either ObjC or Java works fine, and can be used in Swift just fine after the instantiation. It is only calling ImageProvider() in Swift that causes the crash.
Let me know if you have any idea, and thanks again,
-Damian
The question is what the Swift -> ObjC bridge does when you call ImageProvider(). Can we be certain, that it will call the alloc selector followed by the init selector on the returned object? The fact, that a static factory method works around the issue at least suggests, that something unexpected is happening during construction.
We are looking into the issue, we will keep you posted.