0
0
iOS Swiftmobile~20 mins

Predicates and sorting in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Predicate and Sorting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Filtering a list with NSPredicate
Given an array of names, which option correctly filters names starting with the letter 'J' using NSPredicate?
iOS Swift
let names = ["John", "Alice", "Jack", "Bob"]
let predicate = NSPredicate(format: "SELF BEGINSWITH[c] %@", "J")
let filtered = (names as NSArray).filtered(using: predicate) as! [String]
print(filtered)
A["John", "Jack"]
B["Alice", "Bob"]
C["John", "Alice", "Jack", "Bob"]
D[]
Attempts:
2 left
💡 Hint

Look for the predicate that matches names starting with 'J' ignoring case.

📝 Syntax
intermediate
2:00remaining
Correct sorting syntax with NSSortDescriptor
Which option correctly sorts an array of numbers in descending order using NSSortDescriptor?
iOS Swift
let numbers = [3, 1, 4, 2]
let sortDescriptor = NSSortDescriptor(key: nil, ascending: false)
let sorted = (numbers as NSArray).sortedArray(using: [sortDescriptor]) as! [Int]
print(sorted)
A[3, 1, 4, 2]
B[4, 3, 2, 1]
CSyntaxError
D[1, 2, 3, 4]
Attempts:
2 left
💡 Hint

Check the ascending parameter to get descending order.

🔧 Debug
advanced
2:00remaining
Why does this NSPredicate cause a crash?
What error does this code cause and why? let predicate = NSPredicate(format: "age > %@", 18) let filtered = people.filter { predicate.evaluate(with: $0) }
iOS Swift
let predicate = NSPredicate(format: "age > %@", NSNumber(value: 18))
let filtered = people.filter { predicate.evaluate(with: $0) }
ANSInvalidArgumentException because %@ expects an object, not a primitive Int
BNo error, filters correctly
CTypeError because 'age' is undefined
DRuntime crash due to missing key 'age'
Attempts:
2 left
💡 Hint

Check the placeholder %@ and the type of argument passed.

🧠 Conceptual
advanced
2:00remaining
Understanding sorting stability with NSSortDescriptor
If you sort an array of people first by last name ascending, then by first name ascending using two NSSortDescriptors, what is the expected behavior?
AThe array is sorted randomly
BThe array is sorted only by first name, ignoring last name
CThe array is sorted only by last name, ignoring first name
DThe array is sorted by last name, and within same last names, sorted by first name
Attempts:
2 left
💡 Hint

Think about how multiple sort descriptors combine sorting criteria.

lifecycle
expert
2:00remaining
When is NSPredicate evaluated in a Core Data fetch request?
At what point is the NSPredicate applied when fetching data from Core Data?
ANSPredicate is ignored in Core Data fetch requests
BAfter fetching all objects into memory, filtering happens in app code
CWhen the fetch request is executed, filtering happens at the persistent store level
DFiltering happens only when saving data
Attempts:
2 left
💡 Hint

Consider performance and data transfer when fetching from Core Data.