Skip to content

Latest commit

 

History

History
28 lines (20 loc) · 863 Bytes

Companion objects.md

File metadata and controls

28 lines (20 loc) · 863 Bytes

Companion objects

You can access members of Kotlin companion objects from Swift explicitly through the companion auxiliary object: ClassWithCompanionObject.companion.CONST_VAL_EXAMPLE.

Explanations

In Kotlin, working with a companion object is reminiscent of working with static methods and constants in Java. That is:

class CompanionObjectClass {
    companion object {
        const val CONST_VAL_EXAMPLE = "123"
    }
}

fun example() {     
    println(CompanionObjectClass.CONST_VAL_EXAMPLE)
}

In Swift, to access the internals of a companion, there is an object companion that can be accessed through a class:

CompanionObjectClass.companion.CONST_VAL_EXAMPLE

Table of contents