J2ObjC and function returning List /ArrayList types

59 views
Skip to first unread message

Simone Sergi

unread,
Apr 26, 2021, 4:52:29 AM4/26/21
to j2objc-discuss

Hi All,
I'm facing a strange behavior related how function that returns List or ArrayList types are then usable in XCode and swift code.
Can you please help me understand if I'm missing something?

Here is my minimal test case

Basic Java Test method

public static String deserializeStringListtest(String stringIn) {
    return null;
}

transpiler produce the function

#line 71
+ (NSString *)deserializeStringListtestWithNSString:(NSString *)stringIn {
  return <concatenatedPackageName…>_deserializeStringListtestWithNSString_(stringIn);
}

And I can invoke it in swift code easily with
var prova2 = <concatenatedPackageName…>.deserializeStringListtest(with: "aa")


However, changing the return type in the java method, using List or ArrayList

public static List<String> deserializeStringListtest(String stringIn) {
    return null;
}

Transpiled code is still being generated (perhaps, it seems to me) with a correct structure

#line 71
+ (id<JavaUtilList>)deserializeStringListtestWithNSString:(NSString *)stringIn {
  return <concatenatedPackageName…>_deserializeStringListtestWithNSString_(stringIn);
}

But then in swift I start receiving the error
Type '<concatenatedPackageName…>' has no member 'deserializeStringListtest'
As this function do not exists...

I’m making some dummy error or there is really some limit if I use a List/ArrayList return type?

Thank you very much
Simone

Tom Ball

unread,
Apr 26, 2021, 3:11:40 PM4/26/21
to j2objc-discuss
The problem is that the Swift Objective-C importer doesn't support type parameters in protocols, so it just skips them like it does all errors (imagine how much simpler software development would be if we all could do that!). You may be able to workaround this by returning an AbstractList, which most List types extend.

--
You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to j2objc-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/j2objc-discuss/2effaa99-ac09-4e75-ab60-f74ccc402fbbn%40googlegroups.com.

Simone Sergi

unread,
Apr 27, 2021, 8:46:30 AM4/27/21
to j2objc-discuss
Hi Tom and thank you for your feedback!
So, the problem is actually on the coexistence of swift and ObjC and not in the java to ObjC conversion itself...that's incredible :)
In any case, I do not think to have exactly why using AbstractList should workaround it. I've tried but the result seems the same.
As a workaround, instead, I'm trying to convert the list to plain String Array and in this way all seems to work as expected.

Thank you for pointing out this!
Simone

Tom Ball

unread,
Apr 27, 2021, 12:12:40 PM4/27/21
to j2objc-discuss
So, the problem is actually on the coexistence of swift and ObjC and not in the java to ObjC conversion itself...that's incredible :)

Exactly. We've tuned how j2objc generates headers to be as up-to-date as possible with the latest Objective-C standards and what can be done to support Swift, and we are always happy to make future updates. But a quick StackOverflow search for Swift/Objective-C conversion shows there is lots of work that needs to be done (by Apple) in that area. All j2objc is doing is generating headers, so anything that can't be done by an engineer hand-editing a header can't be done with a tool.

We'd love to hear any tips on ways to better declare Objective-C in headers that improves Swift importing. If anyone knows of any, please file an issue with the suggestion.

Reply all
Reply to author
Forward
0 new messages