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

Inside of Swift Export

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for giginet giginet PRO
November 01, 2025

Inside of Swift Export

Kotlin Fest 2025
https://fortee.jp/kotlin-fest-2025/proposal/36866307-c3ca-4cca-b1e4-f0259f293b5f

References:

Swift Export - a peek under the hood - Kotlin Conf 2025
https://2025.kotlinconf.com/talks/798313/
Swift Export: Natural Interoperability between Kotlin and Swift | by Santiago Mattiauda | Medium
https://medium.com/@santimattius/swift-export-natural-interoperability-between-kotlin-and-swift-720799961edc
kotlin/docs/swift-export/architecture.md at master · JetBrains/kotlin
https://github.com/JetBrains/kotlin/blob/master/docs/swift-export/architecture.md

Swift Export Experimental 2.2.20 features
https://kotl.in/swift-export-feature-status

Exploring Kotlin (native) compilation | by Filip Wiesner | MateeDevs | Medium
https://medium.com/mateedevs/exploring-kotlin-native-compilation-7d396b0b7fde
How Kotlin Swift Export Works - YouTube
https://www.youtube.com/watch?v=qtWHRrIfKbQ
Swift Exportはどこから来たのか、Swift Exportは何者か、Swift Exportはどこへ行くのか
https://zenn.dev/f0x/articles/f0d2d1320794bd

Avatar for giginet

giginet PRO

November 01, 2025
Tweet

More Decks by giginet

Other Decks in Programming

Transcript

  1. Swift Export • Kotlin 2.1.0͔Βར༻Մೳʹͳͬͨ৽͍͠Kotlin -> Swiftͷ interoperability(interop) • 2.2.20͔ΒσϑΥϧτ༗ޮԽ

    • ैདྷɺSwift͔Β͸Kotlinͷ࣮૷͕Objective-Cͱ͔ͯ͠͠ݟ͑ͳ͔͕ͬͨɺ௚ ઀Swiftͱͯ͠ѻ͑ΔΑ͏ʹͳͬͨ
  2. Agenda • ैདྷͷSwift Interoperability - Objective-C Export • ࢓૊Έͱ໰୊఺ •

    ৽͍͠Interoperability - Swift Export • ͳʹ͕Ͱ͖ΔΑ͏ʹͳͬͨͷ͔ • Ͳ͏΍ͬͯಈ࡞͍ͯ͠Δͷ͔ • ࠓޙͷϩʔυϚοϓ
  3. fun fibonacci(n: Int): Int = if (n <= 1) n

    else fibonacci(n - 1) + fibonacci(n - 2)
  4. fun fibonacci(n: Int): Int = if (n <= 1) n

    else fibonacci(n - 1) + fibonacci(n - 2) __attribute__((objc_subclassing_restricted)) __attribute__((swift_name("FunctionsKt"))) @interface MOCFFunctionsKt : OCEDBase + (int32_t)fibonacciN:(int32_t)n __attribute__((swift_name("fibonacci(n:)"))); @end
  5. fun fibonacci(n: Int): Int = if (n <= 1) n

    else fibonacci(n - 1) + fibonacci(n - 2) __attribute__((objc_subclassing_restricted)) __attribute__((swift_name("FunctionsKt"))) @interface MOCFFunctionsKt : OCEDBase + (int32_t)fibonacciN:(int32_t)n __attribute__((swift_name("fibonacci(n:)"))); @end import MyObjCFramework FunctionsKt.fibonacci(n: 10)
  6. data class Vector( val x: Double, val y: Double, )

    { operator fun plus(other: Vector): Vector = Vector(x + other.x, y + other.y) }
  7. __attribute__((objc_subclassing_restricted)) __attribute__((swift_name("Vector"))) @interface MOCFVector : MOCFBase - (OCEDVector *)plusOther:(OCEDVector *)other

    __attribute__((swift_name("plus(other:)"))); @end data class Vector( val x: Double, val y: Double, ) { operator fun plus(other: Vector): Vector = Vector(x + other.x, y + other.y) }
  8. __attribute__((objc_subclassing_restricted)) __attribute__((swift_name("Vector"))) @interface MOCFVector : MOCFBase - (OCEDVector *)plusOther:(OCEDVector *)other

    __attribute__((swift_name("plus(other:)"))); @end let a = Vector(x: 10, y: 20) let b = Vector(x: 15, y: 25) a.plus(other: b) data class Vector( val x: Double, val y: Double, ) { operator fun plus(other: Vector): Vector = Vector(x + other.x, y + other.y) }
  9. fun fibonacci(n: Int): Int = if (n <= 1) n

    else fibonacci(n - 1) + fibonacci(n - 2)
  10. fun fibonacci(n: Int): Int = if (n <= 1) n

    else fibonacci(n - 1) + fibonacci(n - 2) import MySwiftFramework fibonacci(n: 10)
  11. package com.github.jetbrains.modulea class ClassFromA(private val name: String) { fun hello():

    String { return "Hello from $name" } } import ModuleA import ModuleB let moduleA = ClassFromA("Alice") let moduleB = ClassFromB("Bob") print(moduleA.hello()) print(moduleB.hello())
  12. data class Vector( val x: Double, val y: Double, )

    { operator fun plus(other: Vector): Vector = Vector(x + other.x, y + other.y) }
  13. data class Vector( val x: Double, val y: Double, )

    { operator fun plus(other: Vector): Vector = Vector(x + other.x, y + other.y) } import Geometry let a = Vector(x: 10, y: 20) let b = Vector(x: 15, y: 25) let combined = a + b
  14. Inside of Swift Export • ΞʔΩςΫνϟ͸ެࣜͷυΩϡϝϯτͰ঺հ͞Ε͍ͯΔ • https://github.com/JetBrains/kotlin/tree/master/docs/swift-export • Reference

    • Swift Export - a peek under the hood - Kotlin Conf 2025 • https://2025.kotlinconf.com/talks/798313/ • Swift Export: Natural Interoperability between Kotlin and Swift | by Santiago Mattiauda | Medium • https://medium.com/@santimattius/swift-export-natural-interoperability- between-kotlin-and-swift-720799961edc
  15. data class Vector( val x: Double, val y: Double, )

    { operator fun plus(other: Vector): Vector = Vector(x + other.x, y + other.y) }
  16. Swift Export Pipeline 1. Analysis APIͰηϚϯςΟοΫΛղੳ 2. Swift IRͷੜ੒ 3.

    SirPrinter / BridgeGeneratorͰϒϦοδΛੜ੒͢Δ 4. Kotlin NativeίϯύΠϥ / SwiftίϯύΠϥͰϏϧυ
  17. Swift Export Pipeline 1. Analysis APIͰηϚϯςΟοΫΛղੳ 2. Swift IRͷੜ੒ 3.

    SirPrinter / BridgeGeneratorͰϒϦοδΛੜ੒͢Δ 4. Kotlin NativeίϯύΠϥ / SwiftίϯύΠϥͰϏϧυ
  18. Swift Export Pipeline 1. Analysis APIͰηϚϯςΟοΫΛղੳ 2. Swift IRͷੜ੒ 3.

    SirPrinter / BridgeGeneratorͰϒϦοδΛੜ੒͢Δ 4. Kotlin NativeίϯύΠϥ / SwiftίϯύΠϥͰϏϧυ
  19. Swift Export Pipeline 1. Analysis APIͰηϚϯςΟοΫΛղੳ 2. Swift IRͷੜ੒ 3.

    SirPrinter / BridgeGeneratorͰϒϦοδΛੜ੒͢Δ 4. Kotlin NativeίϯύΠϥ / SwiftίϯύΠϥͰϏϧυ
  20. Swift Intermediate Representation (Sir) • Swift Exportಠࣗͷதؒදݱ • ࣮ମ͸Swift Export࣮૷಺ͷKotlinΦϒδΣΫτ

    data class Vector constructor(x: Kotlin.Double, y: Kotlin.Double) property x: Kotlin.Double property y: Kotlin.Double operator fun plus(other: Vector): Vector Analysis API
  21. Swift Intermediate Representation (Sir) • Swift Exportಠࣗͷதؒදݱ • ࣮ମ͸Swift Export࣮૷಺ͷKotlinΦϒδΣΫτ

    data class Vector constructor(x: Kotlin.Double, y: Kotlin.Double) property x: Kotlin.Double property y: Kotlin.Double operator fun plus(other: Vector): Vector class Vector init(x: Swift.Double, y: Swift.Double) var x: Swift.Double var y: Swift.Double static func +(this: Vector, other: Vector) -> Vector Analysis API Sir
  22. Swift Export Pipeline 1. Analysis APIͰηϚϯςΟοΫΛղੳ 2. Swift IRͷੜ੒ 3.

    SirPrinter / BridgeGeneratorͰϒϦοδΛੜ੒͢Δ 4. Kotlin NativeίϯύΠϥ / SwiftίϯύΠϥͰϏϧυ
  23. Swift Export Pipeline 1. Analysis APIͰηϚϯςΟοΫΛղੳ 2. Swift IRͷੜ੒ 3.

    SirPrinter / BridgeGeneratorͰϒϦοδΛੜ੒͢Δ 4. Kotlin NativeίϯύΠϥ / SwiftίϯύΠϥͰϏϧυ
  24. Bridgeͷੜ੒ • Sir͔ΒKotlin, Clang Header(C), Swiftͷ3ͭͷίʔυΛੜ੒͢Δ • Kotlin Function Bridge

    • Kotlin NativeͷόΠφϦΛૢ࡞͢ΔͨΊͷ௿ϨϕϧͳΠϯλʔϑΣΠε(ABI) • Swiftଆ͔Β౉͖ͬͯͨϙΠϯλΛKotlinΦϒδΣΫτʹม׵ͯ͠ૢ࡞͢Δϥού • C Header • Kotlin Function BridgeͰग़ྗ͞ΕͨABIΛCͷؔ਺ͱͯ͠ެ։ • Swift • Cͷؔ਺ΛϢʔβʔ͕ૢ࡞͠΍͍͢Α͏ʹSwiftͰϥοϓ
  25. @ExportedBridge("Vector_x_get") public fun Vector_x_get(self: kotlin.native.internal.NativePtr): Double { val __self =

    kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Vector val _result = __self.x return _result } @ExportedBridge("Vector_y_get") public fun Vector_y_get(self: kotlin.native.internal.NativePtr): Double { val __self = kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Vector val _result = __self.y return _result } @ExportedBridge("Vector_plus__TypesOfArguments__SwiftExportDemo_Vector__") public fun Vector_plus__TypesOfArguments__SwiftExportDemo_Vector__(self: kotlin.native.internal.NativePtr, other: kotlin.native.internal.NativePtr): kotlin.native.internal.NativePtr { val __self = kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Vector val __other = kotlin.native.internal.ref.dereferenceExternalRCRef(other) as Vector val _result = __self.plus(__other) return kotlin.native.internal.ref.createRetainedExternalRCRef(_result) } Kotlin Function Bridge (*.kt)
  26. @ExportedBridge("Vector_x_get") public fun Vector_x_get(self: kotlin.native.internal.NativePtr): Double { val __self =

    kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Vector val _result = __self.x return _result } @ExportedBridge("Vector_y_get") public fun Vector_y_get(self: kotlin.native.internal.NativePtr): Double { val __self = kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Vector val _result = __self.y return _result } @ExportedBridge("Vector_plus__TypesOfArguments__SwiftExportDemo_Vector__") public fun Vector_plus__TypesOfArguments__SwiftExportDemo_Vector__(self: kotlin.native.internal.NativePtr, other: kotlin.native.internal.NativePtr): kotlin.native.internal.NativePtr { val __self = kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Vector val __other = kotlin.native.internal.ref.dereferenceExternalRCRef(other) as Vector val _result = __self.plus(__other) return kotlin.native.internal.ref.createRetainedExternalRCRef(_result) } Kotlin Function Bridge (*.kt)
  27. Kotlin Function Bridge (*.kt) @ExportedBridge("Vector_x_get") public fun Vector_x_get(self: kotlin.native.internal.NativePtr): Double

    { val __self = kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Vector val _result = __self.x return _result } @ExportedBridge("Vector_y_get") public fun Vector_y_get(self: kotlin.native.internal.NativePtr): Double { val __self = kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Vector val _result = __self.y return _result } @ExportedBridge("Vector_plus__TypesOfArguments__SwiftExportDemo_Vector__") public fun Vector_plus__TypesOfArguments__SwiftExportDemo_Vector__(self: kotlin.native.internal.NativePtr, other: kotlin.native.internal.NativePtr): kotlin.native.internal.NativePtr { val __self = kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Vector val __other = kotlin.native.internal.ref.dereferenceExternalRCRef(other) as Vector val _result = __self.plus(__other) return kotlin.native.internal.ref.createRetainedExternalRCRef(_result) }
  28. Kotlin Function Bridge (*.kt) @ExportedBridge("Vector_x_get") public fun Vector_x_get(self: kotlin.native.internal.NativePtr): Double

    { val __self = kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Vector val _result = __self.x return _result } @ExportedBridge("Vector_y_get") public fun Vector_y_get(self: kotlin.native.internal.NativePtr): Double { val __self = kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Vector val _result = __self.y return _result } @ExportedBridge("Vector_plus__TypesOfArguments__SwiftExportDemo_Vector__") public fun Vector_plus__TypesOfArguments__SwiftExportDemo_Vector__(self: kotlin.native.internal.NativePtr, other: kotlin.native.internal.NativePtr): kotlin.native.internal.NativePtr { val __self = kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Vector val __other = kotlin.native.internal.ref.dereferenceExternalRCRef(other) as Vector val _result = __self.plus(__other) return kotlin.native.internal.ref.createRetainedExternalRCRef(_result) }
  29. public final class Vector: KotlinRuntime.KotlinBase, KotlinRuntimeSupport._KotlinBridged { public var x:

    Swift.Double { get { return Vector_x_get(self.__externalRCRef()) } } public var y: Swift.Double { get { return Vector_y_get(self.__externalRCRef()) } } public static func +( this: SwiftExportDemo.Vector, other: SwiftExportDemo.Vector ) -> SwiftExportDemo.Vector { this._plus(other: other) } public func _plus( other: SwiftExportDemo.Vector ) -> SwiftExportDemo.Vector { return SwiftExportDemo.Vector.__createClassWrapper(externalRCRef: Vector_plus__TypesOfArguments__SwiftExportDemo_Vector__(self.__externalRCRef(), other.__externalRCRef())) } } Swift Wrapper (*.swift)
  30. public final class Vector: KotlinRuntime.KotlinBase, KotlinRuntimeSupport._KotlinBridged { public var x:

    Swift.Double { get { return Vector_x_get(self.__externalRCRef()) } } public var y: Swift.Double { get { return Vector_y_get(self.__externalRCRef()) } } public static func +( this: SwiftExportDemo.Vector, other: SwiftExportDemo.Vector ) -> SwiftExportDemo.Vector { this._plus(other: other) } public func _plus( other: SwiftExportDemo.Vector ) -> SwiftExportDemo.Vector { return SwiftExportDemo.Vector.__createClassWrapper(externalRCRef: Vector_plus__TypesOfArguments__SwiftExportDemo_Vector__(self.__externalRCRef(), other.__externalRCRef())) } } Swift Wrapper (*.swift)
  31. public final class Vector: KotlinRuntime.KotlinBase, KotlinRuntimeSupport._KotlinBridged { public var x:

    Swift.Double { get { return Vector_x_get(self.__externalRCRef()) } } public var y: Swift.Double { get { return Vector_y_get(self.__externalRCRef()) } } public static func +( this: SwiftExportDemo.Vector, other: SwiftExportDemo.Vector ) -> SwiftExportDemo.Vector { this._plus(other: other) } public func _plus( other: SwiftExportDemo.Vector ) -> SwiftExportDemo.Vector { return SwiftExportDemo.Vector.__createClassWrapper(externalRCRef: Vector_plus__TypesOfArguments__SwiftExportDemo_Vector__(self.__externalRCRef(), other.__externalRCRef())) } } Swift Wrapper (*.swift)
  32. public final class Vector: KotlinRuntime.KotlinBase, KotlinRuntimeSupport._KotlinBridged { public var x:

    Swift.Double { get { return Vector_x_get(self.__externalRCRef()) } } public var y: Swift.Double { get { return Vector_y_get(self.__externalRCRef()) } } public static func +( this: SwiftExportDemo.Vector, other: SwiftExportDemo.Vector ) -> SwiftExportDemo.Vector { this._plus(other: other) } public func _plus( other: SwiftExportDemo.Vector ) -> SwiftExportDemo.Vector { return SwiftExportDemo.Vector.__createClassWrapper(externalRCRef: Vector_plus__TypesOfArguments__SwiftExportDemo_Vector__(self.__externalRCRef(), other.__externalRCRef())) } } Swift Wrapper (*.swift)
  33. Swift Export Pipeline 1. Analysis APIͰηϚϯςΟοΫΛղੳ 2. Swift IRͷੜ੒ 3.

    SirPrinter / BridgeGeneratorͰϒϦοδΛੜ੒͢Δ 4. Kotlin NativeίϯύΠϥ / SwiftίϯύΠϥͰϏϧυ
  34. Swift Export Pipeline 1. Analysis APIͰηϚϯςΟοΫΛղੳ 2. Swift IRͷੜ੒ 3.

    SirPrinter / BridgeGeneratorͰϒϦοδΛੜ੒͢Δ 4. Kotlin NativeίϯύΠϥ / SwiftίϯύΠϥͰϏϧυ
  35. ϩʔυϚοϓ • ObjC Exportͷػೳʹ·ͩ௥͍͍͍ͭͯͳ͍ • ݱஈ֊Ͱ͸Swift Exportಛ༗ͷػೳ΄ͱΜͲͳ͍ • ·ͣObjC ExportͱͷػೳࠩҟΛݮΒ͍ͯ͘͜͠ͱΛ໨ࢦ͍ͯ͠Δ

    • Swift Exportݻ༗ͷػೳ͕௥Ճ͞Ε͍ͯ͘ͷ͸͜Ε͔Β • ݱࡏར༻Մೳͳݴޠػೳ͸ҎԼʹ·ͱ·͍ͬͯΔ • https://kotl.in/swift-export-feature-status
  36. Objective-C Export Swift Export ग़ྗํ޲ ૒ํ޲ Kotlin -> SwiftͷΈ தؒදݱͷੜ੒

    LLVM IRΛ௚઀ੜ੒ K2ͷAnalytics API͔Βઐ༻ͷIRΛ࡞੒ ग़ྗܗࣜ όΠφϦ + ObjCϔομ όΠφϦ + Swiftίʔυ Ϗϧυํ๏ Kotlin Native CompilerͰFrameworkԽ Kotlin Native Compiler + SwiftϏϧυγε ςϜ Gradle Taskͷݺͼग़͠ ୯ମͰ࣮ߦՄೳ XcodeͷBuild Phase͔ΒͷΈ࣮ߦՄೳ ࠷ऴ੒Ռ෺ Apple Framework (*.framework) Swift Package
  37. ·ͱΊ • SwiftίʔυΛੜ੒͢ΔΑ͏ʹͳΓදݱྗ্͕͕ͬͨ • Kotlin NativeͷόΠφϦΛ࣮ߦ͢ΔͨΊͷCϒϦοδΛSwiftͰϥοϓ͢Δ • K2 CompilerͷAnalysis APIΛ࢖ͬͨඪ४తͳ࢓૊ΈͰૄ݁߹ʹͳͬͨɻϏϧ

    υͱϒϦοδͷੜ੒͕੾Γ཭͞Εͨ • Ұ෦ػೳ͸·ͩະ࣮૷ • Xcode্͔Β͔࣮͠ߦͰ͖ͳ͍ • ObjC ExportͰ࣮ݱͰ͖ΔػೳͷҰ෦͕ະ࣮૷ͳͲ
  38. References • Swift Export - a peek under the hood

    - Kotlin Conf 2025 • https://2025.kotlinconf.com/talks/798313/ • Swift Export: Natural Interoperability between Kotlin and Swift | by Santiago Mattiauda | Medium • https://medium.com/@santimattius/swift-export-natural-interoperability- between-kotlin-and-swift-720799961edc • kotlin/docs/swift-export/architecture.md at master · JetBrains/kotlin • https://github.com/JetBrains/kotlin/blob/master/docs/swift-export/ architecture.md
  39. References • Exploring Kotlin (native) compilation | by Filip Wiesner

    | MateeDevs | Medium • https://medium.com/mateedevs/exploring-kotlin-native- compilation-7d396b0b7fde • How Kotlin Swift Export Works - YouTube • https://www.youtube.com/watch?v=qtWHRrIfKbQ • Swift Export͸Ͳ͔͜Βདྷͨͷ͔ɺSwift Export͸Կऀ͔ɺSwift Export͸Ͳ͜ ΁ߦ͘ͷ͔ • https://zenn.dev/f0x/articles/f0d2d1320794bd