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