Reputation: 2448
I have large XML file ( almost 1,27,000 ) bank records.
In one scenario we needed to check Bank Names. e.g.
if(
[title isEqualToString:@"ABHYUDAYA COOPERATIVE BANK LIMITED" ] ||
[title isEqualToString:@"ABU DHABI COMMERCIAL BANK" ] ||
[title isEqualToString:@"AHMEDABAD MERCANTILE COOPERATIVE BANK" ] ){
////Perform certain action
}
Currently we have XML structure and we are parsing this huge data into NSString. So I get above sample data in NSString object.
How to evaluate NSString for above example? On stack overflow found lot of examples of using NSPredicate and NSExpression. But with my understanding of those questions they will handle math expression e.g. 4/3 * 2 etc.. I was unable to process above format using those examples.
Is there way to evaluate above format in NSString and if condition?
PS - Was able to solve above problem by converting data into database but looking for NSString evaluation for String format.
Upvotes: 0
Views: 221
Reputation: 5766
If it is only about making your expression nicer and not changing the underlying logic, you can do this:
NSArray <NSString *> *bankNames = @[@"ABHYUDAYA COOPERATIVE BANK LIMITED"
@"ABU DHABI COMMERCIAL BANK",
@"AHMEDABAD MERCANTILE COOPERATIVE BANK"];
if ([bankNames contains:title]) {
//Perform certain action
}
If you insist on using predicates, then you should do sth like this:
NSArray <NSString *> *bankNames = @[@"ABHYUDAYA COOPERATIVE BANK LIMITED"
@"ABU DHABI COMMERCIAL BANK",
@"AHMEDABAD MERCANTILE COOPERATIVE BANK"];
NSPredicate *containsPredicate = [NSPredicate predicate:@"%K equals %@", @"SELF", title];
BOOl matchesName = [bankNames filteredArrayUsingPredicate:predicate].count > 0;
Nevertheless, I don't really understand why you would want to do that.
Upvotes: 1
Reputation: 1453
Let you have the Data in an NSArray
, named response
, which you want to filter according to the condition.
NSArray *expectedBanks = [NSArray arrayWithObjects:
@"ABHYUDAYA COOPERATIVE BANK LIMITED" ,
@"ABU DHABI COMMERCIAL BANK" ,
@"AHMEDABAD MERCANTILE COOPERATIVE BANK", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"bank IN %@", expectedBanks];
NSArray *filtered = [response filteredArrayUsingPredicate:predicate];
The above code snippet assumes that key for the object ,we need to search in the Array is "bank"
Upvotes: 0