Post

Replies

Boosts

Views

Activity

How to handle predicate with optionals in SwiftData
Xcode: 15.1 I've got (simplified) model with relationship many-to-many defined as follows @Model final class Item { var title: String @Relationship(inverse: \Tag.items) var tags: [Tag]? init(_ title: String) { self.title = title } } @Model final class Tag { var name: String var items: [Item]? init(_ name: String) { self.name = name } } and a view with a query struct ItemsView: View { @Query var items: [Item] var body: some View { List {...} } init(searchText: String) { _items = Query(filter: #Predicate<Item> { item in if (searchText.isEmpty) { return true } else { return item.tags!.contains{$0.name.localizedStandardContains(searchText)} } }) } } This code compiles but fails at runtime with an error: Query encountered an error: SwiftData.SwiftDataError(_error: SwiftData.SwiftDataError._Error.unsupportedPredicate) It looks like Predicate does not like optionals cause after changing tags and items to non optionals and the predicate line to item.tags.contains{$0.name.localizedStandardContains(searchText)} everything works perfectly fine. So, my question is, does anybody know how to make it work with optionals? Full code: https://github.com/m4rqs/PredicateWithOptionals.git
4
1
961
Dec ’23
Sheet doesn't adapt current system colour scheme
Let's assume that default colour system scheme is Light mode. If I open this app and open settings window then change colour scheme to Dark and then change again to System, the main window get back the Light mode but the settings window remains in Dark mode. If I change default colour system scheme is Dark mode, then open this app, open settings window then change colour scheme to Light and then change again to System, the main window get back the Dark mode but the settings window remains in Light mode. What am I doing wrong? Here is simplified code to demo my issue import SwiftUI struct ContentView: View { @State private var isSettingsViewPresented = false @State private var colorScheme = ColorScheme?.none var body: some View { NavigationStack { VStack { Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") } .toolbar { ToolbarItem { Button { isSettingsViewPresented.toggle() } label: { Label("Settings", systemImage: "gear") } } } .sheet(isPresented: $isSettingsViewPresented) { NavigationStack { Form { Picker("Colour Scheme", selection: $colorScheme) { Text("System").tag(ColorScheme?.none) Text("Light").tag(Optional(ColorScheme.light)) Text("Dark").tag(Optional(ColorScheme.dark)) } } .toolbar { ToolbarItem(placement: .confirmationAction) { Button("Done") { isSettingsViewPresented.toggle() } } } } .preferredColorScheme(colorScheme) } } .preferredColorScheme(colorScheme) } }
0
0
191
Nov ’23