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)
}
}
}
}
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hi,
When passing a SwftData Module to a view that receives it in a @Binadable property variable I get an error that it doesn't conform to Observation, I don't know how to solve this issue, even adding @Observable type to the generic in the structure definition won't solve it, any suggestions ?
Kind Regards
Hi,
Im trying to use ForEach without list to get rid of the navigation chevron , but the sipe to delete modifier stop working, is there a some solution ?
Kind Regards
Button(role: .destructive) {
deletePatient(patient: patient)
} label: {
VStack {
Label("Delete", systemImage: "trash")
Image(systemName: "Trash")
}
}
}
Hi
When connecting a SwiftData module property to a SwiftUI view such as a text field and the field changes by user the property get updated in the SwiftData database, now suppose I want to run a validation code or delay updates to Database till use click a submit button how to do that ? delay those auto updates if we can name it ?
Kind Regards
Code Example
import SwiftUI
import SwiftData
struct GListSel2: View {
@Bindable var patient: Patient
var body: some View {
HStack {
TextField("Gender", text: $patient.gender)
}
}
}
Hi,
The dataModule in code below is a swiftData object being passed to the view and its property as key path but when trying to modify it I'm getting the error ."Cannot assign through subscript: 'self' is immutable" how to solve this issue ?
Kind Regards
struct ListSel<T: PersistentModel>: View {
@Bindable var dataModule: T
@Binding var txtValue: String
var keyPath: WritableKeyPath<T, String>
var turncate: CGFloat? = 94.0
var image = ""
var body: some View {
HStack {
Text(txtValue)
.foregroundColor(sysSecondary)
.font(.subheadline)
.onChange(of: txtValue) { value in
dataModule[keyPath: keyPath] = value
}
Image(systemName: image)
.foregroundColor(sysSecondary)
.font(.subheadline)
.imageScale(.small)
.symbolRenderingMode(.hierarchical)
.scaleEffect(0.8)
}
.frame(width: turncate, height: 20, alignment: .leading)
.truncationMode(.tail)
}
}
Hi,
How to customize tables in SwiftUI its color background for example, the background modifier doesn't work ? how to change separator lines ? rows background colors ? give header row different colors to its text and background color ?
Kind Regards
Topic:
UI Frameworks
SubTopic:
SwiftUI
Hi,
Suppose I want to create an School management App that have little complex views so I need a three versions of each view one for Mac and one for iPadOS and one for iOS, cause using platform detector on same view and adjusting layout will make things messy. So what's the best way to achieve that ? Separate project to each platform ? maybe Targets which I still don't understand cause modifying a view in any target will be reflected to other targets so what's the best practice here ? Im working with SwiftUI
Kind Regards
Topic:
UI Frameworks
SubTopic:
SwiftUI
Hi,
Every time Im developing an App and run it the console appears and stay there even when stopping the App then I need to again close it cause it minimize the canvas, is there any setting to prevent it from automatically showing in every run ?
Kind Regards
Topic:
Developer Tools & Services
SubTopic:
Xcode
Hi,
is it possible to prevent App I’m developing from supporting landscape mode on iPhone ? Is there sort of setting in Xcode for that or in code ?
—
kind Regards
Topic:
Developer Tools & Services
SubTopic:
Xcode
Hi,
how to reserve an App name for an App I’m developing ?
—
Kind Regards
Topic:
App Store Distribution & Marketing
SubTopic:
App Store Connect
Hi,
suppose I released an App with subscription price, monthly or annually, can I increase its price anytime ? Or there’s some restrictions ?
—
Kind Regards
Topic:
App Store Distribution & Marketing
SubTopic:
App Store Connect
Hi,
Before the iOS 17.2 update the saving behavior of SwiftData was very straightforward, by default it saves to persistence storage and can be configured to save in memory only. Now it saves to memory by default and to make it save to persistence storage we need to use modelContext.Save(). But if we don't quit the App the changes will be saved after a while to persistence storage even without running modelContext.Save() ! How confusing can that be for both developer and the user ! Am I missing something here ?
--
Kind Regards
Hi
Whats the best way to paste images, links, zip files in the forums ?
Kindest Regards
Hi
App names on App Store is it like domain names ? For example when an App named things I can’t publish an App with same name ? And if yes how to check about available names and reserve them ?
kindest Regards
Topic:
App Store Distribution & Marketing
SubTopic:
App Store Connect
Tags:
App Review
App Store Connect
Hi
can my App now install fonts system wide in iOS and iPadOS ? Is there any helpful links ?
Kindest Regards