AND and OR combined with NSPredicate. IOS Swift

Dynamic predicate or

for my coredata queryget

    var predicate = [NSPredicate]()
    //this three is or
        predicate.append(NSPredicate(format: "item_parent_id = %i", 1))
        predicate.append(NSPredicate(format: "item_parent_id = %i", 3))       
        predicate.append(NSPredicate(format: "item_parent_id = %i", 5))

    // but this one is and
        predicate.append(NSPredicate(format: "price_date CONTAINS[cd] %@", 'timestamp'))

    var compound = NSCompoundPredicate.orPredicateWithSubpredicates(predicate)

      

the first three constraints for the OR operator and the last constraint for the date is AND

I've spent quite a lot of time on this, I'm new to rapid development and I'm not used to Objective C, so it's hard for me to translate what I found written in objective c.

if possible write a response quickly, any comments and suggestions will be of great help. Thank you in advance

obj c - source # 1
obj c - source # 2

+3


source to share


1 answer


If you're looking for a type operation (a OR b OR c) AND d

, you need two compound predicates:



var orList = [NSPredicate]()
//this three is or
orList.append(NSPredicate(format: "item_parent_id = %i", 1))
orList.append(NSPredicate(format: "item_parent_id = %i", 3))       
orList.append(NSPredicate(format: "item_parent_id = %i", 5))
let orPredicate = NSCompoundPredicate.orPredicateWithSubpredicates(orList)

// but this one is and
let other = NSPredicate(format: "price_date CONTAINS[cd] %@", 'timestamp')

let compound = NSCompoundPredicate.andPredicateWithSubpredicates([orPredicate, other])

      

+7


source







All Articles