@ShouldRefineInSwift helps to replace a Kotlin declaration with a wrapper written in Swift. Experimental.
In Kotlin:
interface Person {
@ShouldRefineInSwift
val namePair: Pair<String, String>
}
class RealPerson: Person {
override val namePair = "First" to "Last"
}
In Swift:
extension Person {
var name: (firstName: String, lastName: String) {
let namePair = __namePair
return (namePair.first! as String, namePair.second! as String)
}
}
func shouldRefineInSwiftExample(){
let authorNames = RealPerson().name
print("Author is: \(authorNames.firstName) \(authorNames.lastName)")
}
Using the @ShouldRefineInSwift
annotation, we are indicating that we intend to refine the definition of namePair
in Swift. The annotation marks the property as swift_private in the generated Objective-C API. Such declarations get a __ prefix, which makes them invisible from Xcode’s autocomplete.
Since the annotation is experimental, it is necessary to opt-in.
sourceSets {
all {
languageSettings.optIn("kotlin.experimental.ExperimentalObjCRefinement")
}
}
With thanks to Rick Clephas for the annotation contribution and example code.