https://kotlinlang.org logo
m

MarkRS

08/13/2021, 10:03 AM
I think I need a "multiplatform-getting-started" channel, because this sounds like a very simple question. It probably shows I don't understand. Where, and how, do I include a properly platform specific implementation of a function? I've got a class "GuTime" marked as expected in /shared/src/commonMain. The Android version seems happy in /shared/src/commonAndroid, or probably even /app/src/<etc>, but what about the iOS version? iOS doesn't have "systemtime" so I need an iOS route. I can't put that in /shared/src/commonIOS (can I?), and trying to put an "actual" class in /iOS/ complains about "actual". Yes, I know there's a Kotlin time package on the way, but how "alpha" is it? And that would only help for this specific issue.
m

Michal Klimczak

08/13/2021, 10:53 AM
this is the usual directory structure for multiplatform module. inside every dir you keep the same package structure and then the expect/actual should work just fine (of course if you don;t need tests, you can only keep the 3 "main" dirs)
m

MarkRS

08/13/2021, 11:28 AM
Thanks @Michal Klimczak. I do have that, isn't that simply for the shared module (Kotlin) code, though? Everything I've seen (eg KaMPKit) specifies separate modules for the platform specific code. I don't understand how to link iOS/Swift results into the rest of the shared code. In this example my data controllers sometimes need the time. In Android I get it from the System object. In iOS I seem to need to get a TimeInterval, but how do I feed that back to my shared code?
m

Michal Klimczak

08/13/2021, 11:56 AM
You basicsly have at least 3 codebases (may be in one repo, may be in several). One is ios swift code. One is android module. One is shared module. The shared and android module may be in the same project or in separate. Now expect/actual only refers to the shared module which has these subdirectories which I have shown on screenshot. In "actual" in iosMain you can use objectivec classes, the ios Foundation framework, objc libraries even.
Sometimes it's enough, sometimes it's not. When it's not and you need e.g. to refer to swift code from your shared module, it's a bit more complicated. You need to have some interface which is implemented in swift code (as protocol) and injected back into shared code. But its not something that has to be done very often - only because of lack of kotlin-swoft interoperability (just objc).
More specifically if the TimeInterval you're looking for is a class from Foundation framework (the basic ios stuff) then it should be easily accessible inside iosMain in shared kotlin code
I'm not sure what that is. But for example when I was doing a media player I just used the ios AvAudioPlayer directly in kotlin code.
m

MarkRS

08/13/2021, 12:31 PM
Ok, thanks (again), that should get me going 🙂
👍 1