Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing an Array with another using NSPredicate
    text
    copied!<p>I've the following structure:</p> <pre><code>TxnSummary * t1 = [[TxnSummary alloc] init]; t1.txnId = @"1"; t1.shortDesc = @"First one"; t1.filters = [[NSArray alloc] initWithObjects:@"F1", @"F2", nil]; TxnSummary * t2 = [[TxnSummary alloc] init]; t2.txnId = @"2"; t2.shortDesc = @"Second one"; t2.filters = [[NSArray alloc] initWithObjects:@"F1",@"F2", @"F3", nil]; TxnSummary * t3 = [[TxnSummary alloc] init]; t3.txnId = @"3"; t3.shortDesc = @"Third one"; t3.filters = [[NSArray alloc] initWithObjects:@"F1", @"F3", nil]; TxnSummary * t4 = [[TxnSummary alloc] init]; t4.txnId = @"4"; t4.shortDesc = @"Fourth one"; t4.filters = [[NSArray alloc] initWithObjects:@"F4", nil]; NSArray * xnArray = [[NSArray alloc] initWithObjects:t1,t2,t3,t4, nil]; </code></pre> <p>Now if I want to find out which of the txn summaries have filters F1, then I could do this:</p> <pre><code>NSPredicate * predicate = [NSPredicate predicateWithFormat:@"filters CONTAINS[cd] %@", @"F1"]; NSArray * filteredArray = [xnArray filteredArrayUsingPredicate:predicate]; </code></pre> <p>This works well if I'm comparing for only one string, but if want to find out which all txn summaries have filters "F1", or "F2", then if I have to follow the above mechanism I'll have to create two predicates - each for F1 and F2 and then run it against the xnArray (which seems to be inefficient). I want to be able to create a list of filters strings and use that to fetch the matching txs from the xn array.</p> <pre><code>NSArray * filterStrings = [[NSArray alloc] initWithObjects:@"F1",@"F2", nil]; </code></pre> <p>Does NSPredicate have functionality to achieve this or should I resort to some other method of filtering?</p> <p>Appreciate your help.</p> <p>Thanks, Kumar</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload