Combining 'AND' and 'OR' Condition in NSPredicate

前端 未结 4 1277
南笙
南笙 2020-12-03 04:01

Back again needing more help with constructing my NSPredicates :(

Category
{
   name:string
   subs<-->>SubCategory
}

SubCategory
{
   name:string
         


        
4条回答
  •  既然无缘
    2020-12-03 04:43

    For Swift, this is how you can do it:

    SWIFT 4.x - AND

            let p0 = NSPredicate(format: "X == 1")
            let p1 = NSPredicate(format: "Y == 2")
            fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [p0, p1])
    

    SWIFT 4.x - OR

            let p0 = NSPredicate(format: "X == 1")
            let p1 = NSPredicate(format: "Y == 2")
            fetchRequest.predicate = NSCompoundPredicate(orPredicateWithSubpredicates: [p0, p1])
    

    SWIFT 4.x - NOT

            let p0 = NSPredicate(format: "X == 1")
            let p1 = NSPredicate(format: "Y == 2")
            fetchRequest.predicate = NSCompoundPredicate(notPredicateWithSubpredicates: [p0, p1])
    

    Example

    This is an example from one of my codes, where I look for uid and sync:

            let p0 = NSPredicate(format: self.uid + " = '" + uid + "'")
            let p1 = NSPredicate(format: self.sync + " = %@", NSNumber(value: true as Bool))
            fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [p0, p1])
    

提交回复
热议问题