You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now, for swift types generated by SKIE, a bridge is used between ObjC and Swift with replacement.
To do this, swift types implement Swift._ObjectiveCBridgeable and for Kotlin types in *.apinotes the appropriate type is specified in SwiftBridge.
This logic fine works for Flow and generated enums.
Also, in 0.8.0, it is now possible to write your own swift types.
To write a full-fledged bridge, only a mark in *.apinotes is missing and everything will work!
Example:
Kotlin:
data class TestKotlinResult(val value: String)
Swift in KMP:
public struct TestResult: Swift._ObjectiveCBridgeable {
public let value: String
// For test build in Xcode
public var valueSwift: String { value }
public init(_ value: String) {
self.value = value
}
public typealias _ObjectiveCType = AppKotlinShared.TestKotlinResult
public static func _forceBridgeFromObjectiveC(_ source: _ObjectiveCType, result: inout Self?) -> Swift.Void {
result = fromObjectiveC(source)
}
public static func _conditionallyBridgeFromObjectiveC(_ source: _ObjectiveCType, result: inout Self?) -> Swift.Bool {
result = fromObjectiveC(source)
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: _ObjectiveCType?) -> Self {
return fromObjectiveC(source)
}
public func _bridgeToObjectiveC() -> _ObjectiveCType {
return AppKotlinShared.TestKotlinResult(value: value)
}
private static func fromObjectiveC(_ source: _ObjectiveCType?) -> Self {
guard let source = source else {
fatalError("Couldn't map value of \(Swift.String(describing: source)) to AppKotlinShared.TestResult")
}
return Self(source.value)
}
}
It doesn't work.
But if you find the type after assembly in the *.apinotes and add SwiftBridge, then everything works fine at once, Kotlin type is visible as a wrapper in the form of Swift type. 🎉
Is it possible to add this feature for your custom types?
I believe that without this, such a feature is not fully disclosed.
First of all, this is necessary not for simple types as in the example above, but for types that work with generics.
Such types have a problem when specifying List or Map, the bridge of types helps well.
data class KotlinResponse<T>(val data: T, val requestId: String)
fun getResponse(): KotlinResponse<List<String>>
Without bridge in swift, the return value is seen as KotlinResponse<NSArray> without generic types in List.
With bridge in swift, the return value is seen as SwiftResponse<[String]>.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi!
Now, for swift types generated by SKIE, a bridge is used between ObjC and Swift with replacement.
To do this, swift types implement
Swift._ObjectiveCBridgeable
and for Kotlin types in*.apinotes
the appropriate type is specified inSwiftBridge
.This logic fine works for Flow and generated enums.
Also, in 0.8.0, it is now possible to write your own swift types.
To write a full-fledged bridge, only a mark in
*.apinotes
is missing and everything will work!Example:
Kotlin:
Swift in KMP:
It doesn't work.
But if you find the type after assembly in the
*.apinotes
and add SwiftBridge, then everything works fine at once, Kotlin type is visible as a wrapper in the form of Swift type. 🎉Is it possible to add this feature for your custom types?
I believe that without this, such a feature is not fully disclosed.
First of all, this is necessary not for simple types as in the example above, but for types that work with generics.
Such types have a problem when specifying List or Map, the bridge of types helps well.
Without bridge in swift, the return value is seen as
KotlinResponse<NSArray>
without generic types in List.With bridge in swift, the return value is seen as
SwiftResponse<[String]>
.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions