Sebastien Leclerc Lavallee
05/06/2020, 8:55 PMcompanion
and object
usage inside my Swift code. Let say we have this Kotlin code:
class SomeClass {
companion object {
fun someIntFunction(): Int {
return 1
}
}
}
fun SomeClass.someBoolFunction(): Boolean {
return false
}
fun SomeClass.Companion.toSomeString(): String {
return "to Some String"
}
object SomeOtherStatic {
fun stringStuff(): String {
return "String Stuff"
}
}
And with that Swift code:
let some = SomeClass()
let someBool = some.someBoolFunction()
let someInt = SomeClass.Companion.someIntFunction()
let someString = SomeClass.Companion.toSomeString()
let otherString = SomeOtherStatic.stringStuff()
I get errors from inside Xcode: Instance member 'someIntFunction' cannot be used on type 'SomeClass.Companion'; did you mean to use a value of this type instead?
I have this error for someIntFunction
, toSomeString
and stringStuff
. The compile thinks they are instance functions 🤔
Is there something I am doing wrong?
Thanks!Casey Brooks
05/06/2020, 9:01 PMcompanion
and object
singletons must be accessed through a factory method when called in Swift https://kotlinlang.org/docs/reference/native/objc_interop.html#kotlin-singletonsSebastien Leclerc Lavallee
05/06/2020, 9:14 PMSingleton
basher
05/06/2020, 10:02 PMSomeClass.Companion().toSomeString
Sebastien Leclerc Lavallee
05/06/2020, 10:40 PMsaket
05/07/2020, 12:16 AMSebastien Leclerc Lavallee
05/07/2020, 12:22 AM