I'm digging into ios samples (upd: i read all the ...
# kotlin-native
d
I'm digging into ios samples (upd: i read all the samples, kotlinconf app too), want to understand how they work we have
AppDelegate.h
+
AppDelegate.m
files with stub implementation in
@implementation AppDelegate
we also have AppDelegate.kt:
Copy code
class AppDelegate : UIResponder(), UIApplicationDelegateProtocol
override fun init() = initBy(AppDelegate())
Please, correct my understandings in thread >
1.
initBy(AppDelegate())
- AppDelegate here is the stub declared in
AppDelegate.m
2. what's the
initBy
fun? guess it's a
kotlinx.cinterop.initBy
but I don't understand where the receiver is.
Copy code
@konan.internal.Intrinsic
external fun <T : ObjCObjectBase> T.initBy(constructorCall: T): T
3. Anyway, then you find
initBy
marker with
val objCObjectInitBy = packageScope.getContributedFunctions("initBy").single()
and use it in LLVM IR. 4. The ObjC AppDelegate declaration is needed because no straight Kotlin -> C interop is developed yet. Thus iOS can't easily call
@protocol UIApplicationDelegate
implemented in Kotlin. So you feed the ios with ObjC stub, but placing the kotlin implementation on LLVM level.
s
Last I looked the iOS sample was a bit hackish. I think it improved in the latest release but the sample may not be up to date. The best example to look at is the Kotlinconf app. The source is available on GitHub.
d
well, this concrete part is common among all the samples, including kotlinconf app.
s
4. The ObjC AppDelegate declaration is needed because no straight Kotlin -> C interop is developed yet. Thus iOS can’t easily call
@protocol UIApplicationDelegate
implemented in Kotlin. So you feed the ios with ObjC stub, but placing the kotlin implementation on LLVM level.
No, straight Kotlin -> Objective-C interop is available, and iOS can call the protocol implemented in Kotlin. Objective-C stubs are required only to interact with Xcode. So:
1.
initBy(AppDelegate())
- AppDelegate here is the stub declared in
AppDelegate.m
AppDelegate
here is the class this code is located in.
what’s the
initBy
fun? guess it’s a
kotlinx.cinterop.initBy
but I don’t understand where the receiver is.
The receiver is the same as the receiver of the
init
method being implemented.
initBy
is actually just an intrinsic to call the Kotlin constructor on allocated object instance. It is required because in Objective-C initializers are methods, which can be called virtually. So in some cases, e.g. when iOS allocates and initializes the user-provided class by itself, you need to override some
init*
methods. And
initBy
intrinsic allows to implement these methods by calling the corresponding Kotlin constructors.
d
Wow, thanks, that was a puzzler for me
s
You are welcome! The documentation will come later 🙂