Monday, April 16, 2012

Using characterAtindex with objective C NSPredicate class

I have a pList of strings which I'm bundling into an NSMutable Array. I want to compare each character in the strings to a specified character and use the results to create a new array.



I'm able to filter on the first and last characters using the below



NSPredicate *filter = [NSPredicate predicateWithFormat:@"SELF beginswith[cd] %@", @"B"];

NSPredicate *filter = [NSPredicate predicateWithFormat:@"SELF endswith[cd] %@", @"B"];


but when I try to check the characters in between using the characterAtIndex method I get an error.



NSPredicate *filter = [NSPredicate predicateWithFormat:@"SELF characterAtIndex:%i LIKE[cd] %@", 3, @"B"];


I know I could probably do a loop to loop through each word checking the characters but using NSPredicate is so much tidier and should be much better performance considering the list of strings can be quite large (over 100,000).



Can anyone show me the proper syntax for using characterAtIndex with NSPredicate or have any other solution ideas?



Thanks in advance for any help,
Derm





No comments:

Post a Comment