这是我的NSPredicate:
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!)
我要做的下一件事是:
fetchedResultsController.fetchRequest.predicate = predicate print(fetchedResultsController.fetchRequest.predicate!) try! fetchedResultsController.performFetch() //here is the crash
控制台输出为:
j student.schoolClass.id == 1 AND (book.title CONTAINS[cd] nil OR student.username CONTAINS[cd] "j")
崩溃信息是:
CRASH: Invalid predicate: nil RHS
是不是很奇怪?
在所有当前的iOS和OS X平台上,C int是32位整数,这是该%d格式在变量参数列表中所期望的格式。如果您传递64位整数,则读取下一个变量参数将读取额外的4个零字节。
int
%d
下表显示了哪种整数类型的格式:
格式C型Swift型 ----------------------------------- %d int Int32 %ld long int Int %lld long long int Int64
对于无符号类型也是如此。
或者,将整数转换为NSNumber对象并使用%@格式,这适用于所有大小的整数。例:
NSNumber
%@
let value = 1234 let predicate = NSPredicate(format: "value = %@", value as NSNumber)