我已经看到,在具有关系的查询中,每个谓词都在开头包含单词ANY或ALL (例如: ANY tags.name LIKEc "car"),事实是,如果我删除它(例如: tags.name LIKEc "car"),结果是错误的,或者我得到这样一条消息:无法对对象执行正则表达式匹配。
由于我使用的是NSPredicateEditor,所以它们的值不是ANY或ALL,所以查询总是失败。返回的谓词总是类似于第二个示例(no ANY或ALL)。
为了在我的谓词中添加ANY或ALL,我是否必须将NSPredicateRowTemplateEditor子类化,或者它们是另一种方式?
日期也是一样的。我的日期以以下格式保存: YYYY-MM-DD HH:mm:ss,但NSPredicateEditor使用DD/MM/YYYY,所以每次我尝试日期比较时,它都不起作用。为了改变日期格式,我还需要继承RowEditor的子类吗?
谢谢。
发布于 2016-12-09 02:15:47
这就是了:
class RowTemplateRelationshipAny: NSPredicateEditorRowTemplate {
override func predicate(withSubpredicates subpredicates: [NSPredicate]?) -> NSPredicate{
let predicate: NSComparisonPredicate = super.predicate(withSubpredicates: subpredicates) as! NSComparisonPredicate
let newPredicate = NSComparisonPredicate(leftExpression: predicate.leftExpression, rightExpression: predicate.rightExpression, modifier: .any, type: predicate.predicateOperatorType, options: predicate.options)
return newPredicate
}
}
class RowTemplateRelationshipAll: NSPredicateEditorRowTemplate {
override func predicate(withSubpredicates subpredicates: [NSPredicate]?) -> NSPredicate{
let predicate: NSComparisonPredicate = super.predicate(withSubpredicates: subpredicates) as! NSComparisonPredicate
let newPredicate = NSComparisonPredicate(leftExpression: predicate.leftExpression, rightExpression: predicate.rightExpression, modifier: .all, type: predicate.predicateOperatorType, options: predicate.options)
return newPredicate
}
}只需将IB中的行模板类更改为RowTemplateRelationshipAny或RowTemplateRelationshipAll即可。
https://stackoverflow.com/questions/1415033
复制相似问题