Bird
0
0

You have an array of dictionaries representing people:

hard📝 Application Q15 of 15
iOS Swift - Local Data Persistence
You have an array of dictionaries representing people:
let people = [
  ["name": "John", "age": 30],
  ["name": "Alice", "age": 25],
  ["name": "Bob", "age": 30],
  ["name": "Carol", "age": 25]
]
How do you filter for people aged 25 and sort them by name ascending using NSPredicate and Swift sorting?
AUse NSPredicate(format: "age = 25") and sort with sorted(by: { $0["name"]! > $1["name"]! })
BUse NSPredicate(format: "age >= 25") and sort with sorted(by: { $0["age"]! < $1["age"]! })
CUse NSPredicate(format: "age == 25") and sort with sorted(by: { $0["name"]! < $1["name"]! })
DUse NSPredicate(format: "age == '25'") and sort with sorted(by: { $0["name"]! < $1["name"]! })
Step-by-Step Solution
Solution:
  1. Step 1: Write correct NSPredicate for age 25

    Use "age == 25" to filter exactly age 25; single equals is invalid.
  2. Step 2: Sort filtered array by name ascending

    Sort by comparing names with < operator for ascending order.
  3. Final Answer:

    Use NSPredicate(format: "age == 25") and sort with sorted(by: { $0["name"]! < $1["name"]! }) -> Option C
  4. Quick Check:

    Filter age == 25, sort by name ascending [OK]
Quick Trick: Use == for equality and < for ascending sort [OK]
Common Mistakes:
  • Using single = instead of ==
  • Comparing age as string '25' instead of number
  • Sorting by age instead of name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes