So, basically I have an NSArray.
I want to get an array with the contents of the initial array after having filtered those e.g. NOT beginning by a given prefix.
It think using filteredArrayUsingPredicate: is the best way; but I’m not sure on how I could do it…
This is my code so far (in a NSArray category actually) :
- (NSArray*)filteredByPrefix:(NSString *)pref
{
NSMutableArray* newArray = [[NSMutableArray alloc] initWithObjects: nil];
for (NSString* s in self)
{
if ([s hasPrefix:pref]) [newArray addObject:s];
}
return newArray;
}
Is it the most Cocoa-friendly approach? What I want is something as fast as possible…
Here’s a much simpler way using
filteredArrayUsingPredicate::This filters the array by checking that it matches the string made up of your prefix followed by a wildcard.
If you want to check the prefix case-insensitively, use
like[c]instead.