Combining Two Conditions in Nspredicate

Combining Two Conditions in NSPredicate

Try this

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

NSPredicate with Multiple Conditions

You can try this

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

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];

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 arguments and AND behaviour

To create a predicate (with a variable number of expressions) dynamically, use NSCompoundPredicate, e.g.

let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: subPredicates)

where subPredicates is a [NSPredicate] array, and each subpredicate
is created from the provided arguments in a loop with

NSPredicate(format: "%K == %@", key, value)

Something like this (untested):

func fetchWithPredicate(entityName: String, argumentArray: [NSObject])  -> [NSManagedObject]  {

var subPredicates : [NSPredicate] = []
for i in 0.stride(to: argumentArray.count, by: 2) {
let key = argumentArray[i]
let value = argumentArray[i+1]
let subPredicate = NSPredicate(format: "%K == %@", key, value)
subPredicates.append(subPredicate)
}

let fetchRequest = NSFetchRequest(entityName: entityName)
fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: subPredicates)

// ...
}

NSPredicate on multiple to-many relationships in Core Data

Probably could try SUBQUERY() for NSPredicate.

The code below I came out from some guess and not very sure if it works or not. Usually it takes me trial and error many times for a to-many query clause with NSPredicate.

var predicates = [NSPredicate]()
predicates.append(NSPredicate(format: "SUBQUERY(colours, $colour, ANY $colour.rgb = 13576743).@count > 0"))
let cal = NSCalendar.currentCalendar()
if let xDaysAgo = cal.dateByAddingUnit(.Day, value: -2, toDate: NSDate(), options: []) {
let midnightXDaysAgo = cal.startOfDayForDate(xDaysAgo)
predicates.append(NSPredicate(format: "SUBQUERY(picks, $pick, ALL $pick.pickTime < %@).@count > 0", midnightXDaysAgo))
}

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