Search NSArray of NSDictionary using NSPredicate

user1657861

All, I am trying to do the following and getting no result and not sure where I am going wrong. In my case srchString will be passed from the searchDisplayController so the search string will be dynamic. Just for the heck of it tried hardcoding the value as in the example below and it nothing returned. So looking for some help on how to address this one. Thanks

 (
 {
        amount = "-243.77";
        date = "May 21, 2014";
        description = "Preauthorized Debit";
        extraMap =             {
            MEMO = "ATT              Payment";
            RUNBAL = "$100,159.51";
            id = 123456789454444440000002
        };
        longDate = "2014-05-22";
        referenceId = " ";
        sortDate = "2014-05-22 04:00:00 +0000";
        transactionDateAsCal = "1432267200000:America-New_York";
    },
    {
        amount = "5058.74";
        date = "May 21, 2014";
        description = "Deposit";
        extraMap =             {
            MEMO = "WXYZAWQH          BATCH";
            RUNBAL = "$100,159.51";
            id = 124259821201505220000001;
        };
        longDate = "2014-05-22";
        referenceId = " ";
        sortDate = "2014-05-22 04:00:00 +0000";
        transactionDateAsCal = "1432267200000:America-New_York";
    }
)

NSArray *listOfKeys = [[NSArray alloc]initWithObjects:@"amount", @"date", @"description", @"memo", @"runbal", @"referenceId",@"longDate", @"id", nil];

//Sample test. This search string will be passed from the searchDisplayController.
NSString *srchString = @"Preauthorized";

//If the searchstring has an exact match then it works Like below
//NSString *srchString = @"Preauthorized Debit";

NSMutableArray *subpredicates = [NSMutableArray array];
for (NSString *key in listOfKeys) {
    NSPredicate *subpredicate = [NSPredicate predicateWithFormat:@"%K contains[cd] %@", key, srchString];
    [subpredicates addObject:subpredicate];
}

NSPredicate *resultPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subpredicates];
NSLog(@"predicate %@",resultPredicate);

NSArray *tempArr = [transactionList filteredArrayUsingPredicate:resultPredicate];
NSLog(@"tempArr %@", tempArr);
soulshined

Okay since your not really active with the comments I'll post an answer. Normally I don't like to do this unless I'm sure what the OP is asking for, in this case, I'm assuming I'm confident I understand your vision. First lets talk about what's going on with your example....and lets take away the 'hard coded' portion you shared, I think that will just confuse future question seekers due to grammatical errors and complications.

First thing, your using %K for one predicate, %K is a var arg substitution for a key path. In your instance, you stated you have an array of dictionaries, which ultimately means it's an array, which is not KVO compliant, ergo, will not work.

Secondly, you don't need a loop at all, in fact, I wouldn't encourage it for your circumstance. It can easily be filtered down solely with one predicate.

So lets sum this up for you, and for future question seekers :

Lets say we have an NSArray of NSDictionary as stated above. For example we'll use the data the OP provided above:

NSArray *firstTransactionKeys = [NSArray arrayWithObjects:@"amount",@"date",@"description",@"longdate", nil];
NSArray *firstTransactionValues = [NSArray arrayWithObjects:@"-243.77",@"May 21, 2014",@"Preauthorized Debit",@"2014-05-22 04:00:00", nil];
NSArray *secondTransactionKeys = [NSArray arrayWithObjects:@"amount",@"date",@"description",@"longdate", nil];
NSArray *secondTransactionValues = [NSArray arrayWithObjects:@"5058.74",@"May 21, 2014",@"Deposit",@"2014-05-22 04:00:00", nil];

NSArray *transactionList = [NSArray new];
transactionList = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjects:firstTransactionValues forKeys:firstTransactionKeys], [NSDictionary dictionaryWithObjects:secondTransactionValues forKeys:secondTransactionKeys], nil]; 
NSLog(@"Original Transactions: %@", transactionList);

(
    {
        amount = "-243.77";
        date = "May 21, 2014";
        description = "Preauthorized Debit";
        longdate = "2014-05-22 04:00:00";
    },
        {
        amount = "5058.74";
        date = "May 21, 2014";
        description = Deposit;
        longdate = "2014-05-22 04:00:00";
    }
)

Now that we have the data we want, all we need to do is with code-friendly format, filter the array down to a keyword or search term:

///////Example purposes only. User types in 'Preauthorized Debit'////////
NSString *searchWord = @"Preauthorized Debit";

Now just use an NSPredicate with %@ (var arg for object value).

NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchWord];

Create your new array from the predicate:

NSArray *tempArr = [transactionList filteredArrayUsingPredicate:searchPredicate];
NSLog(@"tempArray = %@", tempArr);

And that's it, hopefully that helps you somewhat grasp things

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using NSPredicate to filter an NSArray of objects based on a property which is an NSDictionary

From Dev

NSPredicate with NSDictionary, NSArray, JSON & HTML data

From Dev

NSPredicate on an NSArray to search for any object

From Dev

Filter a NSArray with another NSArray using NSPredicate

From Dev

Filter NSDictionary by value using NSPredicate

From Dev

How to filter an NSArray using a NSPredicate

From Dev

Fetch NSarray from the NSDictionary of NSArray using objectForKey

From Dev

Sort NSArray of NSDictionary using value

From Dev

NSPredicate search in NSArray containing NSArrays with custom objects

From Dev

NSArray filter using NSPredicate, comparing dates

From Dev

Using NSPredicate on NSArray containing 2 elements

From Dev

Sort NSArray of NSDictionary objects using NSSortDescriptor

From Dev

Using NSPredicate with array for cloudKit search

From Dev

Search from NSArray using Predicate

From Dev

Search from NSArray using Predicate

From Dev

How to filter these NSArray with NSPredicate?

From Dev

NSPredicate in NSArray, contains number with "/"

From Dev

Updating NSDictionary inside an NSArray

From Dev

fill NSArray from NSDictionary

From Dev

Converting NSDictionary to NSArray

From Dev

Mutating NSDictionary inside NSArray

From Dev

Updating NSDictionary inside an NSArray

From Dev

NSDictionary inside NSArray

From Dev

NSDictionary to ordered NSArray

From Dev

Using NSPredicate to search through array of objects

From Dev

Using NSPredicate to search through array of objects

From Dev

Search Controller - Problems using NSPredicate with model

From Dev

Filter NSArray of Object with NSArray of NSNumber with NSPredicate

From Dev

NSPredicate on nested array with NSDictionary as object

Related Related

  1. 1

    Using NSPredicate to filter an NSArray of objects based on a property which is an NSDictionary

  2. 2

    NSPredicate with NSDictionary, NSArray, JSON & HTML data

  3. 3

    NSPredicate on an NSArray to search for any object

  4. 4

    Filter a NSArray with another NSArray using NSPredicate

  5. 5

    Filter NSDictionary by value using NSPredicate

  6. 6

    How to filter an NSArray using a NSPredicate

  7. 7

    Fetch NSarray from the NSDictionary of NSArray using objectForKey

  8. 8

    Sort NSArray of NSDictionary using value

  9. 9

    NSPredicate search in NSArray containing NSArrays with custom objects

  10. 10

    NSArray filter using NSPredicate, comparing dates

  11. 11

    Using NSPredicate on NSArray containing 2 elements

  12. 12

    Sort NSArray of NSDictionary objects using NSSortDescriptor

  13. 13

    Using NSPredicate with array for cloudKit search

  14. 14

    Search from NSArray using Predicate

  15. 15

    Search from NSArray using Predicate

  16. 16

    How to filter these NSArray with NSPredicate?

  17. 17

    NSPredicate in NSArray, contains number with "/"

  18. 18

    Updating NSDictionary inside an NSArray

  19. 19

    fill NSArray from NSDictionary

  20. 20

    Converting NSDictionary to NSArray

  21. 21

    Mutating NSDictionary inside NSArray

  22. 22

    Updating NSDictionary inside an NSArray

  23. 23

    NSDictionary inside NSArray

  24. 24

    NSDictionary to ordered NSArray

  25. 25

    Using NSPredicate to search through array of objects

  26. 26

    Using NSPredicate to search through array of objects

  27. 27

    Search Controller - Problems using NSPredicate with model

  28. 28

    Filter NSArray of Object with NSArray of NSNumber with NSPredicate

  29. 29

    NSPredicate on nested array with NSDictionary as object

HotTag

Archive