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

1

7

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

Erdmann answered 7/7, 2016 at 9:20 Comment(1)
Wondering how you went about doing this? I had the same question.Proper
W
1

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.

Whaleback answered 8/7, 2016 at 5:0 Comment(3)
Thanks for your thorough answer. The native code I need to write is small still I have to learn it. So I would prefer learning something modern than something older. But I understood your point ;-)Erdmann
Is this response up to date? I would like to use a native library for iOS to use the camera, I've seen that several libraries are written in Swift: are not they directly usable? I'm completely new to Xcode, I need something more than the Capture API and more than the Camera Kit cn1Lib.Andromede
Yes but you can still use a compiled library written in Swift since Swift and Objective-C are mostly compatible. Most such libraries can be installed using cocoapods and used "as is"Whaleback

© 2022 - 2024 — McMap. All rights reserved.