From the course: SwiftData Essential Training

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Define predicates

Define predicates

- [Instructor] We've seen how to do many things with arrays. Can we do the same things with SwiftData directly? We can by modifying the query. Those closures in the playgrounds are known as pet predicates such as this one here for filter and those closures return a bool value used to test elements in a collection. I can build my own predicate constants for use in SwiftData. For example, let's head over to the RatingsListView. I'm going to close up my playground, I don't need it anymore, and let's go into RatingsListView and head to the top of the code. And for now, I'm going to put this above the struct. We're going to make it a global and we'll explain more a little later. And I'm going to call it greatRatingsPredicate, so let's do let greatRatingsPredicate. And that's going to equal pound sign Predicate angle brackets, you're going to put in here the type and we're doing a RatingModel. And then inside of here, we're going to put our filter. And in this case, we're going to use…

Contents