Hi! I want to create a Swift class and use it in K...
# multiplatform
t
Hi! I want to create a Swift class and use it in Kotlin in "iosMain". Can I just create both files in iosMain, or do I need to do some extra steps?
k
If you want to literally write swift and have it available to Kotlin, it's much more involved. You'll need to put it in a folder that you can compile and make available somehow. It'll need to be exported to Objective-C with
@objc
(see here: https://kotlinlang.org/docs/native-objc-interop.html). Then you'll need to wire up cinterop from the Kotlin side to make the Swift class visible to Kotlin. Then you'll need to figure out how to package all of that up.
Summary, it's uncommon and non-trivial. We're looking at ways to include generated Swift in the Kotlin framework package, but that's just to help adapt Kotlin to Swift.
👀 1
t
Thanks! I'll try and do that. My use case is very specific: I need to use AWS Amplify with KMM. The Amplify framework generates classes in both Java and Swift. They said they won't bet on KMM, because they're betting on Flutter, so I can't have the classes generated in KMM. So what I'm doing is mapping the Java classes to KMM compatible classes in an automatic way. I was going to do the same for the Swift classes, but I need to have access to these classes from Kotlin
Though I thought I saw in some sample somewhere swift files inside of iosMain
k
kotlin source sets expect kotlin source code. you would need to add your generated swift files to a library with an objective-c bridging header in order to use C interop to add it as a dependency to the iOS target
k
Swift inside iosMain would be pretty great if that was automatic, although I could see that being problematic (dependencies, etc). There's definitely some plumbing involved here, but assuming the Swift is Objc compatible, it should work.
"They said they won't bet on KMM, because they're betting on Flutter" That's an odd official position to take, but OK.
I meant a AWS engineer said something like that on StackOverflow
k
Yeah, I assumed an AWS engineer said that. Just an odd position, especially reading what he wrote. "The Amplify team is currently investing in Flutter as a multi-platform offering." then later "As a final note: the Amplify products are intended as a front-end framework. Therefore, it's unlikely that we we'll ever prioritize a full multi-platform build."
t
Having the swift files with @objc in a cocoapods library, I can use the cocoapods plugin instead of cinterop, right?
k
yes, cocoapods plugin uses cinterop
but dependencies between pods is not supported at this time. so if your pod depends on a pod from amazon, that's not supported
or rather, if the API of your pod depends on classes from another pod
t
Oh okay... That's a problem... My library depends on an Amplify pod
t
But if I use cinterop I have the same problem right? My library depends on the Amplify pod so it won't work
k
correct
if your module doesn't use @import you might be able to get it working
all the details are on the issue
t
I'll take a look, thanks 🙂
Hm I think the Amplify pod is written in Swift and doesn't have @objc headers. That's probably a roadblock huh?
k
yes. cinterop doesn't support swift.
though if you're not using those APIs directly from Kotlin it's not necessarily a roadblock
if you have your own framework that covers the entire API and has obj-c bridging then you should be OK
t
That's the plan