NSPredicateEditor and Relationships
I have seen that every predicate that works in a query with a relation contains the words ANY or ALL at the beginning (ie, ANY .name LIKE [c] "car"), the fact is if I remove it (ie : tag.name LIKE [c] "car"), the result is incorrect, or I get a message like this: Cannot perform regex matching on the object.
Since I am using NSPredicateEditor, they don't have ANY or EVERYTHING that starts my request, so it always fails. The returned predicates are always like the second example (without ANY or ALL).
Do I have to subclass NSPredicateRowTemplateEditor to add myself ANY or ALL to my predicate or is this their other way?
Same with dates ... my dates are stored in this format: YYYY-MM-DD HH: mm: ss, but NSPredicateEditor uses DD / MM / YYYY, so every time I try to compare it doesn't work. I also need to subclass RowEditor to change the date format?
Thank.
source to share
Here you go:
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
}
}
Just change the row template class in IB to RowTemplateRelationshipAny or RowTemplateRelationshipAll.
source to share