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)
}
}
Post
Replies
Boosts
Views
Activity
Hi,
In this year WWDC 2024 conference a new update to SwiftData allows it to connect to other kind of data sources or databases, at ;east this is what I understood, since then didn't see any tutorial or help document on how to do that for example connecting SwiftData to Firebase if possible ?
Kind Regards
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,
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 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, 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)
}
}
}
}
Hi,
Os to possible to customize the icon, color, positioning of the SplitView ToolBar that shows at details view, the menu blue icon. add buttons to it ? change its color, icon, size ? position like adding paddings ?
Kind Regards
Hi,
If Im in detailsOnly mode in SplitView how to manually open the SideBar as popover same as behavior as default rectangle menu icon at top of Details View.
Kind Regards
Hi,
When SplitView is in detailsOnly and I swipe from left side the sidebar appears as popover above the details content, but when I try to open it manually by settings the columnVisibility to doubleColumn it pushes the details view and shows, so haw to make it appear as poorer ?
Kind Regards
Hi,
The new TabBar, SideBar combination is very nice, but I tried customizing like text sizes, icon sizes using imageScale and all dint have any effect, also shows of selected item in side bar couldn't customize it, is there any suggestions ?
Kind Regards
Hi,
However I tried to reduce space between list rows in below code I fail, is there any way to reduce this space ?
@State var flag: Bool = false
var myList: [String] = ["Hello", "World", "SwiftUI"]
var body: some View {
VStack(alignment: .leading) {
List (myList, id: \.self) { list in
Text(list)
.border(Color.black)
.font(.system(size: 12)) // Smaller font size
.frame(maxWidth: .infinity, minHeight: 20, alignment: .leading) // Reduced height
.padding(.vertical, 4) // Minimal padding for vertical spacing
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)) // Remove insets
.listRowSeparator(.hidden) // Hide separator
}
.listStyle(.plain)
}
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(sysSecondary.opacity(0.4), lineWidth: 1)
)
.frame(width: 220, height:260)
.background(.white)
.clipShape (RoundedRectangle (cornerRadius: 10))
}
}
Hi,
I have the below code as you can see the list doesn't hide its line separator, so is it a bug ? is there any work around ?
@State var flag: Bool = false
var myList: [String] = ["Hello", "World", "SwiftUI"]
var body: some View {
VStack(alignment: .leading) {
List (myList, id: \.self) { list in
Text(list)
}
.listRowSeparator(.hidden)
.listStyle(.plain)
}
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(sysSecondary.opacity(0.4), lineWidth: 1)
)
.frame(width: 220, height:260)
.background(.white)
.clipShape (RoundedRectangle (cornerRadius: 10))
}
}
Hi,
Is there anyway to auto format a text field to show mobiles numbers properly like a modifier or something ? and is there an API to check if the text is a valid mail address not just dummy text ?
--
Kind Regards
Hi,
The example that Apple shows in link below doesn't show how truncationMode works, because the text get truncated anyway when its length it more than frame height so what's the use of truncationMode ?
https://developer.apple.com/documentation/swiftui/view/truncationmode(_:)
--
Kind Regards
Hi,
Is it possible to change font color of dark text in DatePicker in SwiftUI ? Also can we change size of arrows on top right ?
Kind Regards