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);
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.
Comments