I have a pList of strings which I’m bundling into an NSMutable Array.
NSString *path = [[NSBundle mainBundle] pathForResource:@"small" ofType:@"plist"];
_availableWords = [[NSMutableArray alloc]initWithContentsOfFile:path];
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?
Update: Based on Kens answer below I am now using the below code to check characters outside of the first and last characters. For example the below checks the 4th character.
NSArray *matches = [_availableWords objectsAtIndexes:[_availableWords indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop){
return (BOOL)([obj characterAtIndex:3] == 'R');
}]];
However, when I go to inspect the matches array in the debugger I see the below error
matches NSArray * 0x06a67ba0 @"Variable is not a CFArray"
and no objects in the array. Based on the sample data in short.pList I’d expect to see one object in the array.
Thanks in advance for any help,
Derm
The built in predicates don’t support such an operation. However, you can build a predicate which evaluates a block:
Or, if you’re just going to turn around and filter an array with this predicate, you can cut out the middleman and use a block to identify elements of an array directly: