From 5b63cdb4b2be360bf25828a23bdee41b2255dddc Mon Sep 17 00:00:00 2001 From: Alexander Prendota Date: Fri, 14 Sep 2018 17:03:25 +0300 Subject: [PATCH] add kotlin-playground styles to OBJC_INTEROP.md (#2064) --- OBJC_INTEROP.md | 62 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/OBJC_INTEROP.md b/OBJC_INTEROP.md index 83c7c6b6717..4bdf3b886ec 100644 --- a/OBJC_INTEROP.md +++ b/OBJC_INTEROP.md @@ -68,35 +68,54 @@ Kotlin constructors are imported as initializers to Swift/Objective-C. Top-level Kotlin functions and properties are accessible as members of a special class. Each Kotlin package is translated into such a class. E.g. -``` + +
+ +```kotlin package my.library fun foo() {} ``` +
+ can be called from Swift like -``` + +
+ +```swift Framework.foo() ``` +
+ ### Method names translation Generally Swift argument labels and Objective-C selector pieces are mapped to Kotlin parameter names. Anyway these two concepts have different semantics, so sometimes Swift/Objective-C methods can be imported with clashing Kotlin signature. In this case clashing methods can be called from Kotlin using named arguments, e.g.: -``` + +
+ +```swift [player moveTo:LEFT byMeters:17] [player moveTo:UP byInches:42] ``` +
+ in Kotlin would be: -``` +
+ +```kotlin player.moveTo(LEFT, byMeters = 17) player.moveTo(UP, byInches = 42) ``` +
+ ### Errors and exceptions Kotlin has no concept of checked exceptions, all Kotlin exceptions are unchecked. @@ -160,35 +179,54 @@ as corresponding `Unit` singleton in Swift/Objective-C. The value of this single can be retrieved in the same way as for any other Kotlin `object` (see singletons in the table above). To sum the things up: -``` + +
+ +```kotlin fun foo(block: (Int) -> Unit) { ... } ``` +
+ would be represented in Swift as -``` +
+ +```swift func foo(block: (NSNumber) -> KotlinUnit) ``` +
+ and can be called like -``` + +
+ +```kotlin foo { bar($0 as! Int32) return KotlinUnit() } ``` +
+ ## Casting between mapped types When writing Kotlin code, an object may require to be converted from Kotlin type to equivalent Swift/Objective-C type (or vice versa). In this case plain old Kotlin cast can be used, e.g. -``` + +
+ +```kotlin val nsArray = listOf(1, 2, 3) as NSArray val string = nsString as String val nsNumber = 42 as NSNumber ``` +
+ ## Subclassing ### Subclassing Kotlin classes and interfaces from Swift/Objective-C @@ -211,13 +249,19 @@ overriding method must have the same parameter names as the overridden one. Sometimes it is required to override initializers, e.g. when subclassing `UIViewController`. Initializers imported as Kotlin constructors can be overridden by Kotlin constructors marked with `@OverrideInit` annotation: -``` + +
+ +```swift class ViewController : UIViewController { @OverrideInit constructor(coder: NSCoder) : super(coder) ... } ``` + +
+ The overriding constructor must have the same parameter names and types as the overridden one. To override different methods with clashing Kotlin signatures, one can add