An NSPredicate is a way to filter collections or Core Data fetch requests by defining conditions that items must meet. Think of it like a filter that only lets through items matching certain rules.
Click to reveal answer
beginner
How do you create a predicate to find names starting with "A"?
You create it like this: NSPredicate(format: "name BEGINSWITH[c] %@", "A"). This means it looks for names starting with 'A', ignoring case.
Click to reveal answer
beginner
What is NSSortDescriptor used for?
NSSortDescriptor tells Swift how to sort a list of items. For example, you can sort by a property like 'age' in ascending or descending order.
Click to reveal answer
intermediate
How do you sort an array of people by age descending using NSSortDescriptor?
You create a sort descriptor like this: NSSortDescriptor(key: "age", ascending: false) and use it to sort the array.
Click to reveal answer
intermediate
Why use predicates and sort descriptors together?
Using predicates filters your data to only what you want, and sort descriptors organize that filtered data in the order you want. Together, they help you get exactly the right data, nicely ordered.
Click to reveal answer
What does NSPredicate do in Swift?
AFilters data based on conditions
BSorts data in ascending order
CCreates user interface elements
DManages memory allocation
✗ Incorrect
NSPredicate is used to filter data by specifying conditions.
Which of these is the correct way to create a predicate to find items where 'age' is greater than 18?
ANSPredicate(format: "age > 18")
BNSPredicate(format: "age >= 18")
CNSPredicate(format: "age < 18")
DNSPredicate(format: "age == 18")
✗ Incorrect
The predicate format "age > 18" filters for ages greater than 18.
What does NSSortDescriptor(key: "name", ascending: true) do?
AFilters items with key 'name'
BFilters items with name 'true'
CSorts items by name in descending order
DSorts items by name in ascending order
✗ Incorrect
It sorts items by the 'name' property in ascending order.
How can you combine multiple sort descriptors?
AChain them with '+' operator
BPut them in an array and apply together
CUse only one sort descriptor at a time
DUse NSPredicate instead
✗ Incorrect
Multiple NSSortDescriptors can be combined in an array to sort by multiple keys.
Which is true about predicates and sorting?
ABoth sort data
BPredicates order data; sorting filters data
CPredicates filter data; sorting orders data
DBoth filter data
✗ Incorrect
Predicates filter data by conditions; sorting arranges data order.
Explain how you would filter a list of contacts to only those whose names start with 'J' and then sort them by last name ascending.
Think about how to write the predicate format and then create a sort descriptor.
You got /4 concepts.
Describe the difference between NSPredicate and NSSortDescriptor and how they work together in data handling.
Imagine filtering a list first, then arranging it.
You got /3 concepts.
Practice
(1/5)
1. What does an NSPredicate do in Swift when working with arrays?
easy
A. Removes duplicates from the array
B. Sorts the array in ascending or descending order
C. Adds new items to the array
D. Filters the array to include only items matching a condition
Solution
Step 1: Understand the role of NSPredicate
An NSPredicate is used to specify conditions to filter collections like arrays.
Step 2: Differentiate filtering from sorting
Filtering means selecting items that meet a condition, while sorting changes order.
Final Answer:
Filters the array to include only items matching a condition -> Option D
Quick Check:
Predicate = Filter [OK]
Hint: Predicate means filter, not sort or add [OK]
Common Mistakes:
Confusing predicate with sorting
Thinking predicate adds or removes items
Assuming predicate changes order
2. Which of the following is the correct syntax to create an NSPredicate filtering for names equal to "John"?
easy
A. NSPredicate(format: "name == 'John'")
B. NSPredicate(format: "name = 'John'")
C. NSPredicate(format: "name equals 'John'")
D. NSPredicate(format: "name === 'John'")
Solution
Step 1: Recall NSPredicate format syntax
NSPredicate uses format strings with == for equality comparison.
Step 2: Identify correct equality operator
== is the correct operator; = or === are invalid in NSPredicate format.
Final Answer:
NSPredicate(format: "name == 'John'") -> Option A
Quick Check:
Use == for equality in NSPredicate [OK]
Hint: Use double equals (==) for equality in predicates [OK]
Common Mistakes:
Using single equals (=) instead of double (==)
Using === which is not valid in NSPredicate
Writing 'equals' as a word instead of operator
3. Given this Swift code snippet, what will be the output?
let names = ["Anna", "John", "Bob", "Alice"]
let predicate = NSPredicate(format: "SELF BEGINSWITH[c] 'A'")
let filtered = (names as NSArray).filtered(using: predicate) as! [String]
let sorted = filtered.sorted(by: { $0 > $1 })
print(sorted)
medium
A. ["Alice", "Anna"]
B. ["Anna", "Alice"]
C. ["Anna"]
D. ["Alice"]
Solution
Step 1: Filter names starting with 'A' ignoring case
The predicate selects names beginning with 'A' or 'a': "Anna" and "Alice".
Step 2: Sort filtered names descending
Sorting with $0 > $1 means descending order alphabetically: "Alice" comes before "Anna".
Final Answer:
["Alice", "Anna"] -> Option A
Quick Check:
Filter by 'A', then descending sort = ["Alice", "Anna"] [OK]
Hint: Filter first, then sort; descending means bigger first [OK]
Common Mistakes:
Sorting ascending instead of descending
Ignoring case sensitivity in predicate
Confusing filter and sort order
4. Identify the error in this Swift code using NSPredicate and sorting:
let numbers = [5, 3, 8, 1]
let predicate = NSPredicate(format: "SELF > 4")
let filtered = numbers.filter { predicate.evaluate(with: $0) }
let sorted = filtered.sorted(by: { $0 < $1 })
print(sorted)
medium
A. NSPredicate format string is incorrect
B. No error; code works as expected
C. Filtering with predicate.evaluate(with:) is correct
D. Cannot use NSPredicate with Int values
Solution
Step 1: Check NSPredicate format
The format "SELF > 4" is valid for filtering numbers greater than 4.
Step 2: Verify filtering and sorting logic
Using predicate.evaluate(with:) inside filter is valid; sorting ascending is correct.
Final Answer:
No error; code works as expected -> Option B
Quick Check:
Predicate with Int and filter works fine [OK]
Hint: Evaluate predicate inside filter is valid for Int arrays [OK]
Common Mistakes:
Thinking NSPredicate can't filter Ints
Assuming format string is wrong
Expecting error in sorting syntax
5. You have an array of dictionaries representing people: