Predicates & Sort Descriptors
Filter and order fetched results.
Predicates & Sort Descriptors is a free SwiftUI Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Shaping Your Results
As data grows you rarely want every row. Predicates and sort descriptors let you fetch just the right slice.
What a Predicate Is
An NSPredicate is a rule that decides which records match, written in a compact query-like format string.
NSPredicate(format: "isDone == true")Comparing Values
Use operators like ==, >, and < in the format string to match numbers, dates, and other fields.
NSPredicate(format: "priority > 2")Safe Arguments
Insert dynamic values with %@ placeholders instead of string-building, which is safer and clearer.
NSPredicate(format: "title == %@", searchText)Text Matching
Match partial text with CONTAINS, and add [c] to ignore case for friendly search.
NSPredicate(format: "title CONTAINS[c] %@", query)Combining Conditions
Join rules with AND and OR to express more specific filters in one predicate.
NSPredicate(format: "isDone == false AND priority > 1")What a Sort Descriptor Is
A SortDescriptor tells Core Data which attribute orders the rows, and in which direction.
SortDescriptor(\.title, order: .forward)Multiple Sort Keys
Pass an array of descriptors to break ties, like sorting by priority first, then by name.
[SortDescriptor(\.priority), SortDescriptor(\.title)]Using Them in @FetchRequest
Combine both in a @FetchRequest to fetch a filtered, ordered set right inside your view.
@FetchRequest(sortDescriptors: [SortDescriptor(\.title)], predicate: NSPredicate(format: "isDone == false")) var tasks: FetchedResults<Task>Filtering Beats Looping
Letting the store filter and sort is faster than fetching everything and trimming it in Swift. ⚡
Update Filters Live
You can rebuild a predicate from a search field and reassign it, so results refine as the user types.
Quick Check
You can now filter and order data. Quick check on the tools.
Recap
You used predicates to filter records and sort descriptors to order them, fetching exactly the data each screen needs. ✨
Frequently asked questions
Is the “Predicates & Sort Descriptors” lesson free?
Yes — the full text of “Predicates & Sort Descriptors” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Predicates & Sort Descriptors”?
Filter and order fetched results. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Predicates & Sort Descriptors” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Core Data Stack
- Fetching with @FetchRequest
- Creating & Saving Entities
- Predicates & Sort Descriptors