Hey new to the KMP. I’m playing around with Swift ...
# multiplatform
r
Hey new to the KMP. I’m playing around with Swift UI and Observable-ViewModel + Native Coroutines. To begin with the journey I’ve developed an empty ViewModel extending the Observable-ViewModel. Whenever I try to add the
@StateViewModel
to the ViewModel in my SwiftUI the XCode reports the following error:
Generic struct 'StateViewModel' requires that 'DetailViewModel' conform to 'ViewModel'
More details on the 🧵
Swift UI adding the
ViewModel
Copy code
import SwiftUI
import Shared
//import KMPNativeCoroutinesAsync
import KMPObservableViewModelSwiftUI

struct ContentView: View {
    @StateViewModel
    var viewModel = DetailViewModel()

    var body: some View {
        EmptyView()
    }
}
The
ViewModel
extending Observable-ViewModel
Copy code
import com.rickclephas.kmp.observableviewmodel.ViewModel

class DetailViewModel : ViewModel() {
}
Requirements seems to be in place
Copy code
plugins {
    alias(libs.plugins.kmpNativeCoroutines)
    alias(libs.plugins.ksp)
}

commonMain.dependencies {
  api(libs.kmp.observable.viewmodel)
}

all {
  languageSettings.optIn("kotlin.experimental.ExperimentalObjCName")
}
Dependency versions:
Copy code
kmpNativeCoroutines = "1.0.0-ALPHA-33"
kmpObservableViewmodel = "1.0.0-BETA-3"
ksp = "2.0.0-1.0.23"
kotlin = "2.0.0"
And swift package dependencies:
I cannot seem to find the root cause. There must be some misconfiguration or something else that I’m definitely missing. I’ve tried other sample projects and it’s working nicely. However for the project I’ve started on my own. Doesn’t seem to even start off
r
Based on the error message it seems you are missing the
KMPObservableViewModel.swift
file with the extension:
Copy code
import KMPObservableViewModelCore
import shared // This should be your shared KMP module

extension Kmp_observableviewmodel_coreViewModel: ViewModel { }
r
Ah, of course! I missed it 🤦‍♂️
Huh, the issue still shows up. Do we need to do anything else? Like set it in a specific directory or clean something?
r
Nope just somewhere in the iOS target. I don't think a clean build would be required, but it can't hurt.
👍 1
r
Finally made it work! I forgot I made a last-minute change using the original Android ViewModel. Indeed the extension was the file I needed. Thank you Rick 🙇 Will continue the KMM journey 🙌
👍🏻 1
128 Views