SkipUI works by leveraging transformations performed by the Skip transpiler. Because SwiftUI uses View objects and Compose uses @Composable functions, the transpiler performs two key transformations:
- State Syncing: It inserts code to synchronize SwiftUI state members (like
@State or @EnvironmentObject) with Compose state mechanisms. This synchronization is bidirectional. - ViewBuilder Transformation: It converts
@ViewBuilder blocks (including View.body) into ComposeBuilder objects.
Inside a ComposeBuilder, the original logic of the body is executed within a @Composable block. Any View that would have been returned in SwiftUI is instead rendered using the Compose(context:) function, which is part of SkipUI's View API. This allows the transpiler to bridge the declarative models of both frameworks.
struct V: View {
let isHello: Bool
var body: some View {
if isHello {
Text("Hello!")
} else {
Text("Goodbye!")
}
}
}
// Transpiled Kotlin equivalent:
class V(val isHello: Boolean) : View() {
override fun body(): View {
return ComposeBuilder { composectx ->
if (isHello) {
Text("Hello!").Compose(context = composectx)
} else {
Text("Goodbye!").Compose(context = composectx)
}
ComposeResult.ok
}
}
}