I want to get strings that have specific length in NSArray.
The array has many elements and I don't want to use fast enumeration.
Is there a possible way?
I want to get strings that have specific length in NSArray.
The array has many elements and I don't want to use fast enumeration.
Is there a possible way?
No matter what you do you will be using fast enumeration whether you realize it or not. However, have you considered using an NSPredicate object and the filteredArrayWithPredicate
method?
This works like a charm:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.length == %d", lenght];
NSArray *filtered = [array filteredArrayUsingPredicate:predicate];
NSArray *yourArray = [[NSArray alloc] initWithObjects:@"Apple, Orange, Grapes, Cherry, nil"];
for(NSString *element in yourArray){
if(element.length==yourLength){
[filteredArray addObject:element];
}
}
NSLog(@"Filtered array now contains the elements with length %d", yourLength);
NSLog(@"Filtered array--%@", filteredArray);