简体   繁体   中英

NSPredicateEditor and relationships

I've seen that every predicate that works in a query with a relationship contains at the start the words ANY or ALL (ie: ANY tags.name LIKE[c] "car"), the fact is, if I remove it (ie: tags.name LIKE[c] "car"), the result is wrong or I get a message like this one : Can't do regex matching on object.

Since i'm using an NSPredicateEditor their is no ANY or ALL that starts my query, so it always fail. The Predicates returned is always like the second exemple (no ANY or ALL).

Do I have to subclass the NSPredicateRowTemplateEditor, in order to add myself the ANY or ALL in my predicate, or is their another way?

Same thing with the dates... my dates are saved in this format: YYYY-MM-DD HH:mm:ss, but the NSPredicateEditor use DD/MM/YYYY, so each time I try a date comparaison, it does not work. Do I also have to subclass the RowEditor, in order to change the date format?

Thank you.

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 your row template class in IB to RowTemplateRelationshipAny or RowTemplateRelationshipAll.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM