Is Kotlin Multi-platform Mobile code different from compiled Swift code on iOS platform? - ios

According to docs
Kotlin/Native is a technology for compiling Kotlin code to native
binaries, which can run without a virtual machine. It is an LLVM based
backend for the Kotlin compiler and native implementation of the
Kotlin standard library.
So K/N is using LLVM to compile Kotlin code to native code for iOS. Swift compiler also uses LLVM to optimize and generate machine code.
So is there any difference between compiled K/N vs compiled Swift code on iOS platform?
If so what are those differences? Performance and etc.

The main difference is that Kotlin code compiled for iOS brings along the Kotlin/Native runtime which establishes the Kotlin/Native memory model that aims for safe concurrency and immutability of global objects, and also provides its own garbage collector for Kotlin objects.
Otherwise, code compiled with Kotlin/Native indeed has two-way interoperability with Swift, as normally the Kotlin binary that you use on iOS is an Objective-C framework that you can import in one project with Swift.

Related

How to prebuilt libraries to be compatible with future swift versions

We are prebuilding some libraries (mainly with carthage) for some of our projects to reduce development times. These libraries are not updated very often, but we want to update our XCode versions pretty fast.
Now every time a new XCode brings a new swift version, we are seeing this incompatibility issue
File.swift:4:8: error: module compiled with Swift 5.3.2 cannot be imported by the Swift 5.4 compiler: /......./Debug-iphoneos/Alamofire.framework/Modules/Alamofire.swiftmodule/arm64-apple-ios.swiftmodule
How can I pre-build my dependencies in a way that a swift update wont affect it and I dont have to re-build the dependencies with every xcode update (I thought thats what ABI stability was for? How can I activate that?)
It sounds like you're misunderstanding what ABI stability enables. The main benefit is that it allows the OS to include only one version of the Swift standard library, and for all Swift binaries to share it. What you want is "module stability". From the docs on ABI stability:
ABI stability is about mixing versions of Swift at run time. What
about compile time? Right now, Swift uses an opaque archive format
called “swiftmodule” to describe the interface of a library, such as a
framework “MagicKit”, rather than manually-written header files.
However, the “swiftmodule” format is also tied to the current version
of the compiler, which means an app developer can’t import MagicKit if
MagicKit was built with a different version of Swift. That is, the app
developer and the library author have to be using the same version of
the compiler.
To remove this restriction, the library author needs a feature
currently being implemented called module stability. This involves
augmenting the opaque format with a textual summary of a module,
similar to what you see in Xcodeʼs “Generated Interface” view, so that
clients can use a module without having to care what compiler it was
built with.
This is not yet supported in any version of Swift.

iOS: Binary Framework using Swift

If I make binary framework using swift, will it support objective C projects and what will be minimum supported version? is there any other drawbacks compare to objective c framework?
Yes, Binary frameworks in Swift will support Obj-C projects. Please ensure Obj-C related settings in Build Settings of Framework project are enabled. It will be enabled by default to create bridging header to inter-operate with Obj-C.
Minimum supported OS Version
With Swift ABI stability achieved in Swift 5 and now, module stability in place, there is no need to ship Swift source code while sharing the frameworks. If we want to share the library, Binary framework (xcframework) can be created and shipped without worrying about Swift compiler version incompatibility errors.
Since binary frameworks is dependent on module stability, and in turn, ABI stability, it needs Swift standard library in OS itself. Apple started including Swift standard library in OS when Xcode 10.2 was released, in which Swift 5 was supported. It means, iOS < 12.2 will not have Swift standard library. So, in case of iOS version < 12.2, all swift apps will have their own runtime library included in it. In case of pure Obj-C projects running in iOS < 12.2, ensure "Always Embed Swift Standard Libraries" to Yes in Build settings. Otherwise, it will crash in runtime. So, with respect to OS versions, we see this as the only limitation.
I was able to run a pure Obj-C project using Binary Framework in Swift, even in iOS 9 simulator.
Advantages of using Binary Framework
Binary Framework in swift can be created for all devices, simulators and for all platforms (iOS, MacOS, watchOS, tvOS). It's a single library for all platforms.
We need not ship actual swift source code in framework.It has resolved Swift compiler incompatibility issues.
Interoperability with Obj-C is simple and effortless and can't vouch same on vice-versa.
Hope it helps.

Is Mono framework or mono runtime part of the Xamarin IOS ipa package?

Basically, my question is if Xamarin IOS is statically compiled or compiled ahead of time (AOT), why do we need Mono Runtime within ipa?
May be I do not understand how does xamarin ios work in the ios devices?
Please can anybody shed some light?
When you compile any Xamarin platform application, the Mono C# (or F#)
compiler will run and will compile your C# and F# code into Microsoft
Intermediate Language (MSIL). If you are running a Xamarin.Android, a
Xamarin.Mac application, or even a Xamarin.iOS application on the
simulator, the .NET Common Language Runtime (CLR) compiles the MSIL
using a Just in Time (JIT) compiler. At runtime this is compiled into
a native code, which can run on the correct architecture for your
application.
However, there is a security restriction on iOS, set by Apple, which
disallows the execution of dynamically generated code on a device. To
ensure that we adhere to these safety protocols, Xamarin.iOS instead
uses an Ahead of Time (AOT) compiler to compile the managed code. This
produces a native iOS binary, optionally optimized with LLVM for
devices, that can be deployed on Apple’s ARM-based processor.
iOS App Architecture this link may be useful to you for more detail.
why do we need Mono Runtime within ipa?
The native ARM code that is generated by Xamarin.iOS (mtouch) AOT process depends upon the Mono runtime as the AOT process is not a transcoding compiler.
This dependency is mainly for the garbage collector but it also provides the runtime interface from the low level .Net/Mono framework calls to the iOS|tvOS|watchOS operating system.

How Does Dart AOT Work?

In my search for how Dart AOT works, I have not found many resources except this video. I would like to know how it is that code can be compiled down to native machine code, such as Android or iOS, when there exists different pieces of hardware that code needs to run on.
From what I understand, there are only descriptions of apps produced in Flutter. That description (written in Dart) is then compiled down to native machine code, but how? A program written in Swift is different from a program written in Kotlin.
A compiler creates the binary code from Dart source code.
For mobile applications the source code is compiled for multiple processors ARM, ARM64, x64 and for both platforms - Android and iOS. This means there are multiple resulting binary files for each supported processor and platform combination.
From what I understand, there are only descriptions of apps produced in Flutter.
Not sure what you mean by that. The concept of source code and compilation to a target platform is basically the same for each programming language.
JIT (Just in Time) compiles at runtime on-the-fly while AOT (Ahead of Time) compiles before the application is deployed and launched.
A program written in Swift is different from a program written in Kotlin.
Also not sure what you mean by that.
Swift can compile to native code and Java to Java bytecode. Swift is AoT while Java is JiT. The end result is always binary code for the target platform and CPU.

using an ios framework for native c++ code for ios

I have written a c++ library that needs opencv which is an image processing library. I want to now use this c++ library on ios. To do that I am going to copy my code to a mac and build to produce a cocoa touch static library.
Since, this has a dependency on opencv, I downloaded its ios framework. But now I am confused whether a framework can be used from c++ code or just from objective c/c++ ? Do I have to recompile this library so that i get c++ libraries or I can use the framework in my c++ code?
Yes, it can be used with c++. You will have to make sure you Type is set to "Objective C++ Source" for where you are making the framework calls.
I mix my C++ code with frameworks all the time.
Note this goes both ways. If you have Obj-C interacting with C++, you'll need to either have the file be a .mm or be of the "Objective C++ Source" Type.
The Type selection is in the File Inspector for files.

Resources