“无效的谓词:nil RHS” NSPredicate格式的第二个参数 [英] "Invalid predicate: nil RHS" for second argument in NSPredicate format
问题描述
这是我的 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
Isn'这很奇怪吗?
Isn't it weird?
推荐答案
在所有当前的iOS和OS X平台上,C int
是一个32位整数,
,这是变量参数列表中%d
格式所期望的。
如果传递一个64位整数,那么读取下一个变量参数
将读取额外的4个零字节。
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.
或者,将整数转换为一个 NSNumber
对象并使用
%@
格式,对于所有整数,这都是
大小。示例:
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)
这篇关于“无效的谓词:nil RHS” NSPredicate格式的第二个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!