Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
791 views
in Technique[技术] by (71.8m points)

ios - "Invalid predicate: nil RHS" for second argument in NSPredicate format

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

On all current iOS and OS X platforms, the C int is a 32-bit integer, and that is what the %d format expects 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.

Alternatively, convert the integer to a NSNumber object and use the %@ format, this works for integers of all sizes. Example:

let value = 1234
let predicate = NSPredicate(format: "value = %@", value as NSNumber)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...