https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
t

Tiago Nunes

05/18/2021, 4:37 PM
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

kpgalligan

05/18/2021, 4:48 PM
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

Tiago Nunes

05/18/2021, 4:56 PM
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

Kris Wong

05/18/2021, 4:59 PM
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

kpgalligan

05/18/2021, 4:59 PM
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

kpgalligan

05/18/2021, 5:08 PM
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

Tiago Nunes

05/19/2021, 5:16 PM
Having the swift files with @objc in a cocoapods library, I can use the cocoapods plugin instead of cinterop, right?
k

Kris Wong

05/19/2021, 5:32 PM
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

Tiago Nunes

05/19/2021, 5:39 PM
Oh okay... That's a problem... My library depends on an Amplify pod
t

Tiago Nunes

05/19/2021, 5:42 PM
But if I use cinterop I have the same problem right? My library depends on the Amplify pod so it won't work
k

Kris Wong

05/19/2021, 5:42 PM
correct
if your module doesn't use @import you might be able to get it working
all the details are on the issue
t

Tiago Nunes

05/19/2021, 5:45 PM
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

Kris Wong

05/19/2021, 7:58 PM
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

Tiago Nunes

05/19/2021, 9:05 PM
That's the plan