Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
iOS Swift - Local Data Persistence
Identify the error in this code snippet:
let predicate = NSPredicate(format: "age > %@", 18)
let filtered = people.filter { predicate.evaluate(with: $0) }
ANSPredicate format string is incorrect
BMissing closing parenthesis in filter
CUsing %@ with an Int value causes a runtime error
Devaluate(with:) cannot be used in filter
Step-by-Step Solution
Solution:
  1. Step 1: Check NSPredicate format and arguments

    Using %@ expects an object, but 18 is an Int literal, causing a type mismatch.
  2. Step 2: Understand correct usage

    Use %d or cast 18 to NSNumber to avoid error.
  3. Final Answer:

    Using %@ with an Int value causes a runtime error -> Option C
  4. Quick Check:

    Use correct format specifier for Int [OK]
Quick Trick: Use %d for Int in NSPredicate format strings [OK]
Common Mistakes:
  • Using %@ for Int values
  • Ignoring type mismatch errors
  • Assuming evaluate(with:) is invalid in filter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes