Bartłomiej Semańczyk Bartłomiej Semańczyk - 7 months ago 117
Swift Question

NSPredicate - "CRASH: Invalid predicate: nil RHS"

This is my

NSPredicate
:

print(searchTextField.text!)
let predicate = NSPredicate(format: "student.schoolClass.id = %d AND (book.title CONTAINS[cd] %@ OR student.username CONTAINS[cd] %@)", currentSchoolClass.id, searchTextField.text!, searchTextField.text!)


The next thing I do is:

fetchedResultsController.fetchRequest.predicate = predicate
print(fetchedResultsController.fetchRequest.predicate!)
try! fetchedResultsController.performFetch() //here is the crash


The console output is:

j
student.schoolClass.id == 1 AND (book.title CONTAINS[cd] nil OR student.username CONTAINS[cd] "j")


And crash info is:

CRASH: Invalid predicate: nil RHS


Isn't it weird?

Answer

The %d format expects a 32-bit integer on the variable argument list. If you pass a 64-bit integer then reading the next variable argument will read the extra 4 zero bytes.

The following table shows which format is for which integer type:

  Format   C type          Swift type
  -----------------------------------
  %d       int             Int32
  %ld      long int        Int
  %lld     long long int   Int64

and similarly for the unsigned types.