Hi, I have the view below that I want it to get any sort of SwiftData model and display and string property of that module, but I get the error mentioned below as well, also the preview have an error as below, how to fix it ? I know its little complicated.
- Error for the view GWidget
" 'init(wrappedValue:)' is unavailable: The wrapped value must be an object that conforms to Observable "
- Error of preview
" Cannot use explicit 'return' statement in the body of result builder 'ViewBuilder' "
3, Code
#Preview {
do {
let configuration = ModelConfiguration(isStoredInMemoryOnly: true)
let container = try ModelContainer(for: Patient.self, configurations: configuration)
let example = Patient(firstName: "Ammar S. Mitoori", mobileNumber: "+974 5515 7818", homePhone: "+974 5515 7818", email: "ammar.s.mitoori@gmail.com", bloodType: "O+")
// Pass the model (Patient) and the keyPath to the bloodType property
return GWidget(model: example, keyPath: \Patient.bloodType)
.modelContainer(container)
} catch {
fatalError("Fatal Error")
}
}
4. Code for the Gwidget View
```import SwiftUI
import SwiftData
struct GWidget<T>: View {
@Bindable var model: T
var keyPath: KeyPath<T, String> // Key path to any string property
// Variables for the modified string and the last character
var bloodTypeWithoutLast: String {
let bloodType = model[keyPath: keyPath]
return String(bloodType.dropLast())
}
var lastCharacter: String {
let bloodType = model[keyPath: keyPath]
return String(bloodType.suffix(1))
}
var body: some View {
VStack(alignment: .leading) {
Text("Blood Type")
.font(.footnote)
.foregroundStyle(sysPrimery07)
HStack (alignment: .lastTextBaseline) {
Text(bloodTypeWithoutLast)
.fontWeight(.bold)
.font(.title2)
.foregroundStyle(sysPrimery07)
VStack(alignment: .leading, spacing: -5) {
Text(lastCharacter)
.fontWeight(.bold)
Text(lastCharacter == "+" ? "Positive" : "Negative")
}
.font(.caption2)
.foregroundStyle(lastCharacter == "+" ? .green : .pink)
}
}
}
}
" 'init(wrappedValue:)' is unavailable: The wrapped value must be an object that conforms to Observable "
With this message, the compiler is telling you that model
is not Observable
. You can fix that by telling the compiler that T
is a SwiftData model type, as shown below:
struct GWidget<T: PersistentModel>: View {
@Bindable var model: T
" Cannot use explicit 'return' statement in the body of result builder 'ViewBuilder' "
I don't see this error on my side. I guess this will go away after you fix the first error.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.