Combining 'And' and 'Or' Condition in Nspredicate

Combining Two Conditions in NSPredicate

Try this

request.predicate = NSPredicate(format: "username = %@ AND password = %@", txtUserName.text!, txtPassword.text!)

Combining 'AND' and 'OR' Condition in NSPredicate

I'll do the following using a SUBQUERY.

[NSPredicate predicateWithFormat@"(name == %@) AND (SUBQUERY(subs, $x, $x.numbervalue == %@ or $x.numbervalue == %@).@count > 0)", @"aname", value1, value2];

Try it and let me know.

Another solution could be to grab the whole collection subs and then filter the retrieved set with a predicate to check if elements match for value1 or value2.

Hope it helps.

Edit

If you follow Eimantas suggestion make to sure to create the right array:

NSNumber valueA = [NSNumber numberWithInt:20];
NSNumber valueB = [NSNumber numberWithInt:90];
NSArray *arr = [NSArray arrayWithObjects:valueA, valueB, nil];

Combining And and Or on NSPredicates

I don't have enough reputation to post a comment. What you have posted should work. It might help to post more of your code to identify the exact issue.

In hopes that it helps you catch your issue, I'll post a working sample with combined AND and OR:

let alice = Person(firstName: "Alice", lastName: "Smith", age: 24, position: "Up")
let bob = Person(firstName: "Bob", lastName: "Jones", age: 27, position: "Left")
let charlie = Person(firstName: "Charlie", lastName: "Smith", age: 33, position: "Right")
let quentin = Person(firstName: "Quentin", lastName: "Alberts", age: 31, position: "Down")
let people = [alice, bob, charlie, quentin]

let predicate = NSPredicate(format: "firstName contains[cd] %@ AND (position == %@ OR position == %@)", "e", "Up", "Down")
let filtered = (people as NSArray).filteredArrayUsingPredicate(predicate)

print(filtered)

// prints [Alice Smith, Quentin Alberts]

Swift combining predicates LogicalType AND OR

NSCompoundPredicate is a subclass of NSPredicate, which means
that the result of
NSCompoundPredicate(type:subpredicates:) can be used in another compound
predicate.

Note however that the %@ format placeholder expects an NSObject
instance:

let deptPredicate = NSPredicate(format: "dept == %@", 1 as NSNumber)
let subdeptPredicate1 = NSPredicate(format: "subdept = %@", 11 as NSNumber)
let subdeptPredicate2 = NSPredicate(format: "subdept = %@", 12 as NSNumber)

let orPredicate = NSCompoundPredicate(type: .or,
subpredicates: [subdeptPredicate1, subdeptPredicate2])

let andPredicate = NSCompoundPredicate(type: .and,
subpredicates: [deptPredicate, orPredicate])

Alternatively, use the %ld format for integers:

let deptPredicate = NSPredicate(format: "dept == %ld", 1)
// ... etc.

There are also convenience initializers:

let orPredicate = NSCompoundPredicate(orPredicateWithSubpredicates:
[subdeptPredicate1, subdeptPredicate2])

let andPredicate = NSCompoundPredicate(andPredicateWithSubpredicates:
[deptPredicate, orPredicate])

Compound predicates are very useful to combine a dynamic set of
conditions at runtime. On the other hand, if only the values change
then you can simply use "AND" and "OR" within the predicate
format string:

NSPredicate(format: "dept == %ld AND (subdept = %ld OR subdept = %ld)", 1, 11, 12)

Finally note that you can use the #keyPath directive with the
%K placeholder, so that the compiler fills in the correct property
name (thus reducing the chance of typographical errors):

let deptPredicate = NSPredicate(format: "%K == %ld", #keyPath(MyEntity.dept), 1)

NSPredicate with Multiple Conditions

You can try this

NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[p1, p2]];

AND and OR combined with NSPredicate. IOS Swift

If you're looking for an operation like (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])

Swift combining predicates LogicalType AND OR

NSCompoundPredicate is a subclass of NSPredicate, which means
that the result of
NSCompoundPredicate(type:subpredicates:) can be used in another compound
predicate.

Note however that the %@ format placeholder expects an NSObject
instance:

let deptPredicate = NSPredicate(format: "dept == %@", 1 as NSNumber)
let subdeptPredicate1 = NSPredicate(format: "subdept = %@", 11 as NSNumber)
let subdeptPredicate2 = NSPredicate(format: "subdept = %@", 12 as NSNumber)

let orPredicate = NSCompoundPredicate(type: .or,
subpredicates: [subdeptPredicate1, subdeptPredicate2])

let andPredicate = NSCompoundPredicate(type: .and,
subpredicates: [deptPredicate, orPredicate])

Alternatively, use the %ld format for integers:

let deptPredicate = NSPredicate(format: "dept == %ld", 1)
// ... etc.

There are also convenience initializers:

let orPredicate = NSCompoundPredicate(orPredicateWithSubpredicates:
[subdeptPredicate1, subdeptPredicate2])

let andPredicate = NSCompoundPredicate(andPredicateWithSubpredicates:
[deptPredicate, orPredicate])

Compound predicates are very useful to combine a dynamic set of
conditions at runtime. On the other hand, if only the values change
then you can simply use "AND" and "OR" within the predicate
format string:

NSPredicate(format: "dept == %ld AND (subdept = %ld OR subdept = %ld)", 1, 11, 12)

Finally note that you can use the #keyPath directive with the
%K placeholder, so that the compiler fills in the correct property
name (thus reducing the chance of typographical errors):

let deptPredicate = NSPredicate(format: "%K == %ld", #keyPath(MyEntity.dept), 1)

NSCompoundPredicate Mixing AND and OR

Ok to mix "AND" and "OR" in a compound Predicate I did this...

if ([searchCriteria isEqualToString:@"All"]) {
[parr addObject:[NSPredicate predicateWithFormat:@"aID_Name like[cd] %@",mySearchBar.text]];
[parr addObject:[NSPredicate predicateWithFormat:@"aTrackingNum like[cd] [parr addObject:[NSPredicate predicateWithFormat:@"aRegNum like[cd] %@",mySearchBar.text]];
predicate = [NSCompoundPredicate orPredicateWithSubpredicates:parr];
}

[parr removeAllObjects];
[parr addObject:predicate];
if (aGroups)
[parr addObject:[NSPredicate predicateWithFormat:@"SUBQUERY(self.group, $x, $x.gID_Key IN %@).@count > 0",[aGroups valueForKey:@"gID_Key"]]];

predicate = [NSCompoundPredicate andPredicateWithSubpredicates:parr];
NSLog(@"%@",predicate);


Related Topics



Leave a reply



Submit