NSPredicate:无法解析格式字符串
NSPredicate: Unable to parse the format string
我写了下面的谓词,但它使应用程序崩溃,原因是 'Unable to parse the format string "(rootActivityId IN 12,13 AND (activityType == 3 OR activityType == 2)) OR (activityId IN 12,13 AND (activityType == 3 OR activityType == 2))"'
let activityTypeClause = "(activityType == \(NSNumber(integer: ActivityType.TypeTwo)) OR activityType == \(NSNumber(integer: ActivityType.TypeOne)))"
fetchRequest.predicate = NSPredicate(format: "(rootActivityId IN \(activityIds) AND \(activityTypeClause)) OR (activityId IN \(activityIds) AND \(activityTypeClause))")
// activityIds contains coma separated string
我不知道我哪里做错了。
来自 IN 的文档:
Equivalent to an SQL IN operation, the left-hand side must appear in
the collection specified by the right-hand side. For example, name IN
{ 'Ben', 'Melissa', 'Nick' }. The collection may be an array, a set,
or a dictionary—in the case of a dictionary, its values are used.
这表明逗号分隔的字符串实际上应该是一个集合对象。我不清楚是否需要 NS
风格的 Swift 集合。
不评论它是否作为谓词起作用,但以下不会给我解析错误:
let rootActivityId = "12"
let activityIds = ["12", "13"]
let pred = NSPredicate(format: "rootActivityId IN %@", argumentArray: [activityIds])
我写了下面的谓词,但它使应用程序崩溃,原因是 'Unable to parse the format string "(rootActivityId IN 12,13 AND (activityType == 3 OR activityType == 2)) OR (activityId IN 12,13 AND (activityType == 3 OR activityType == 2))"'
let activityTypeClause = "(activityType == \(NSNumber(integer: ActivityType.TypeTwo)) OR activityType == \(NSNumber(integer: ActivityType.TypeOne)))"
fetchRequest.predicate = NSPredicate(format: "(rootActivityId IN \(activityIds) AND \(activityTypeClause)) OR (activityId IN \(activityIds) AND \(activityTypeClause))")
// activityIds contains coma separated string
我不知道我哪里做错了。
来自 IN 的文档:
Equivalent to an SQL IN operation, the left-hand side must appear in the collection specified by the right-hand side. For example, name IN { 'Ben', 'Melissa', 'Nick' }. The collection may be an array, a set, or a dictionary—in the case of a dictionary, its values are used.
这表明逗号分隔的字符串实际上应该是一个集合对象。我不清楚是否需要 NS
风格的 Swift 集合。
不评论它是否作为谓词起作用,但以下不会给我解析错误:
let rootActivityId = "12"
let activityIds = ["12", "13"]
let pred = NSPredicate(format: "rootActivityId IN %@", argumentArray: [activityIds])