Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can swift code be used in Codename One native code instead of Objective-C

I need to insert native code in my Codename One app. I am a completely newbie in iOS programming so I need to learn Objective-C. However I read that Swift was the successor of Objective-C and I would feel more comfortable with Swift syntax. But I don't know if Swift is supported in Codename One. While looking at iOS source code generated by Codename One, although I am not 100% sure, it looks like C code (I may be wrong).

On his blog Shannah (from Codename One) wrote that

Codename One currently compiles everything down to plain old C code so we would be largely unaffected by such a change – although there would be some native portions that use Objective-C that would need updating. Luckily, if you’re a Codename One user, you don’t need to concern yourself with these details because you are working in Java.

I don't get it clear that's why I am asking whether it is possible to use Swift code in Codename One native code ?

Thanks a lot for helping me clarifying this topic!

Cheers

like image 459
HelloWorld Avatar asked Jul 07 '16 09:07

HelloWorld


1 Answers

Yes and no. You can probably compile Swift code to a static library today (.a file) and just use that like any static lib where the Objective-C code just acts as a bridge.

Using Swift directly is problematic due to several factors:

  • Currently we are still using an older version of xcode when building, we made an attempt at migrating to the latest but had a setback. We'll migrate hopefully before 3.5 comes out, if not then shortly after.
    Swift requires a relatively new version of xcode so until we do that embedding Swift code will be problematic.

  • Swift assumes ARC. This is something we tried to integrate with the GC but at this time it doesn't really work well.

  • We need to generate Swift stubs as an option, this is problematic as say you have existing cn1lib or native interface that relies on Objective-C it might create a conflict.

But lets back track a bit. Swift is a huge advantage over Objective-C which is pretty old by now. But it doesn't have any major advantages for Codename One developers...

  • It isn't faster as Codename One translates to C which is faster than both Swift and Objective-C

  • It won't make the code much cleaner, if you have a lot of code in your native interfaces then you are doing something wrong. Most of your code is in Java anyway, you can debate the merits of Java vs. Swift but if you are using Codename One then you pretty much picked Java.

  • If you need to use an app written in Swift you can package it as a static library.

So there is really no real use case for Swift in Codename One at this time.

like image 128
Shai Almog Avatar answered Sep 21 '22 20:09

Shai Almog