Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Development on Kotlin/Native and Swift

Aydar
January 10, 2022

Development on Kotlin/Native and Swift

Presented on mDevMeet 2021

Aydar

January 10, 2022
Tweet

More Decks by Aydar

Other Decks in Programming

Transcript

  1. Kotlin Multiplatform Mobile 9 class MyClass { fun hello() =

    "Hello from Kotlin!" } let a = MyClass() Problems of an iOS-developer
  2. Kotlin Multiplatform Mobile 10 / / . . . _

    _ attribute __ ((swift_name("KotlinDouble"))) @interface KNFDouble : KNFNumber - (instancetype)initWithDouble:(double)value; + (instancetype)numberWithDouble:(double)value; @end; _ _ attribute __ ((swift_name("KotlinBoolean"))) @interface KNFBoolean : KNFNumber - (instancetype)initWithBool:(BOOL)value; + (instancetype)numberWithBool:(BOOL)value; @end; _ _ attribute __ ((objc_subclassing_restricted)) _ _ attribute __ ((swift_name("MyClass"))) @interface KNFMyClass : KNFBase - (instancetype)init _ _ attribute __ ((swift_name("init()"))) __ attribute __ ((objc_designated_initializer)); + (instancetype)new __ attribute _ _ ((availability(swift, unavailable, message="use object initializers instead"))); - (NSString *)hello __ attribute _ _ ((swift_name("hello()"))); @end; Problems of an iOS-developer
  3. Kotlin Multiplatform Mobile 12 • Lack of Swift and Obj-C

    support in Android Studio • Necessity to rebuild the framework every time you change the interface • Complicated relation between declaration in Kotlin and usage in Swift Problems of an iOS-developer
  4. 13

  5. Kotlin/Native plugin for AppCode 2018 The fi rst release Development

    on Kotlin/Native in AppCode Highlighting, autocompletion Run | Debug iOS Integration with a wizard 14
  6. What is a symbol Symbol • Name • Location •

    Attributes • Relations with other symbols • … 18 Internal representation of the declaration of anything in the code. Symbol tables - the IDE's knowledge of classes, functions, variables, etc.
  7. class Person { let name = "John" let surname =

    "Doe" let age = 42 // Very important function func greet() { print("Hi \(name)!") } } class Person let name: String let surname: String let age: Int func greet() -> Void Symbol table 19 What is a symbol
  8. Obj-C - Swift Cross Resolve Obj-C class AnotherObject: NSObject @interface

    AnotherObject : NSObject //... @end Naming Rules 20
  9. MyShared.framework MyFramework.h MyFramework IDE Old approach val helloString = "Hello

    from Kotlin!" @property (readonly) NSString *helloString; objc var helloString: String { get } Kotlin compiler ⏳ Build project 23 Symbols translation
  10. Kotlin compiler 24 Kotlin frontend Kotlin/Native backend Kotlin 
 Code

    Module Descriptors MyShared.framework MyFramework.h MyFramework Symbols translation
  11. IDE New approach val helloString = "Hello from Kotlin!" var

    helloString: String { get } Internal Compiler Structures @property (readonly) NSString *helloString; objc New 26 Symbols translation
  12. Advantages of the new approach 🚀 Updates on the fl

    y 🎯 Go to Declaration, refactorings and etc. 😅 Forgives mistakes 27 Symbols translation
  13. Hooray! 
 Now you don't have to rebuild the framework

    after each Kotlin code change! 28 print(a.hello) SourceKit
  14. 30 val helloString = "Hello!" Create property SourceKit import MyFramework

    Open Swift fi le Getting annotations from SourceKit Problem
  15. 31 Open Swift usage print(a.hello) val hello = "Hello!" Rename

    val helloString = "Hello!" Create property print(a.helloString) Use in Swift @property (readonly) NSString *helloString; MyFramework.h Build SourceKit Getting annotations from SourceKit Problem
  16. 32 📁 MyFramework.framework/ 📁 Headers/ MyFramework.h 📁 Modules/ module.modulemap Info.plist

    MyFramework SourceKit Getting annotations from SourceKit Solution
  17. 33 📁 MyFramework.framework/ 📁 Headers/ MyFramework.h 📁 Modules/ module.modulemap SourceKit

    After each Kotlin code change ~/Library/Caches/…/DerivedData/MyApp-byxnfgfeahmhpthjejgycztlpstg/SourceKitFrameworkStubs/MyFramework.framework Getting annotations from SourceKit Solution
  18. Optimizations 34 • Generate right before the opening of a

    Swift fi le • Ignore changes in comments and function bodies / / This is very important fun doImportantThings(): Int { val a = 1 val b = 2 return a + b } Getting annotations from SourceKit
  19. 37 Cross-resolve between Kotlin and Swift already exists, and you

    can try it right now in Kotlin/Native plugin for AppCode Conclusions
  20. 38 In the KMP world, it is very useful to

    know Gradle, even if you develop iOS apps Conclusions
  21. Thank you! 39 What we talked about: • KMP intro

    • Current problems of the iOS world • Kotlin/Native plugin for AppCode • How Kotlin-Swift cross-resolve works • SourceKit problem and how it’s solved • Conclusions @appcode @aydarmukh