I have a simple class Service in commonMain which I can instantiate and use in an Android/Kotlin sub...
g
I have a simple class Service in commonMain which I can instantiate and use in an Android/Kotlin submodule. When I try to do the same in Swift I get this error: "ios/Runner/AppDelegate.swift2821: error: 'init()' is unavailable" and also: "common.Serivce1324: note: 'init()' has been explicitly marked unavailable here public convenience init()". I'm a total Swift noob and pretty clueless what's happening. Help? 😅
g
Maybe a mistake between an exported method named "init()" and the Swift init method? (init/deinit are keywords in Swift AFAIK) https://docs.swift.org/swift-book/LanguageGuide/Initialization.html
g
I found the issue and it really was a dumb mistake showing how little I know of Swift. This does not work for Swift:
Copy code
class Service(
    client: CloudClient? = null
){
but changing it to this and no problemo:
Copy code
class Service {
    private val mdexClient = CloudClient()
}
so I think the former results in code for swift where there is no init() method to create a new instance where the latter does
r
Swift doesn't see Kotlin's default parameters. So the first one probably works if you pass in a client (or nil) manually, but it won't work if you call it with no arguments.
g
I tried with passing in nil manually but it didn't work. But it could be I made a mistake because I'm a Swift noob 🙂
s
@russhwolf is that documented anywhere? Curious about other interop gotchas
r
General interop stuff is documented at https://kotlinlang.org/docs/native-objc-interop.html
👍 1
🔥 1
s
Ty sir
b
@russhwolf seems like you have worked our this 🙂
r
If you want to call from swift without passing args, you need to define a no-arg version in Kotlin instead of relying on default parameters. You can do it as a secondary constructor if you want to leave the existing default parameters behavior for Kotlin consumers. Something like
Copy code
data class Builder(
        var inputs: List<String> = emptyList(),
        var concat: String? = "",
    ) {
        constructor() : this(emptyList(), "")
        ...
    }
b
Ok! I will give that a shot !
Saved me loads of time. TYTYTTYTYT
👍 1