Search code examples
swiftj2objc

j2objc Unable to call method that returns JavaIoInputStream in swift


One of my java classes implements 2 methods:

public OutputStream getOutputStream(){
    return null;
}

public int getV(){
    return 3;
}

After conversion with j2Objc, I try to call those methods in swift:

func test() {
    let i = ComExampleTestSharedMyClass()
    var v = d.getV()
    var s = d.getOutputStream()
}

Instantiation and getV() compiles pretty well but getOutputStream() doesn't with the message:

ComExampleTestSharedMyClass has no member getOutputStream.

In MyClass.h, I can see both translated methods:

- (jint)getV;
- (JavaIoOutputStream *)getOutputStream;

The obvious difference is about the returned type which is part of jre_emul.

For testing purpose, I did a test in objective-C (my first attempt!):

#import "Configurator-Bridging-Header.h"

@implementation TestGetOutputStream

- (void) theTest{
    ComExampleTestSharedMyClass* i = [ComExampleTestSharedMyClass alloc];
    [i getOutputStream];
}
@end

It compiles with objective-C, so what can I do for swift?


Solution

  • OK, I was fooled by the compiler message saying that the class doesn't have the requested method while, in fact, it's the return type of the method that was unknown.

    It works if I add #import "OutputStream.h" in the bridging header.