My NSMutableArray
contains some strings as elements. One of the element is repeated many times at different indexes in the array. For example [@"", @"1,2,3",@"",@"5,3,2,1",@""]
.
I want to remove all the elements with value @""
from the mutable array. I tried following ways but couldn't get the solution.
Using For loop:
for(id obj in myMutableArray)
{
if([obj isEqualToString:@""])
{
[myMytableArray removeObject:obj];
}
}
Using dummy mutable array called nextMutableArray
for(id obj in myMutableArray)
{
if([obj isEqualToString:@""])
{
continue;
}
else [nextMutableArray addObject:obj];
}
In both the ways, elements (@""
) at other indexes are removed but not at the index 0 (first object). What could be the possible reason? Is there any way to remove all the elements that contain string @""
from the mutable array?
one option is to filter your array using predicates:
NSArray *someArray = @[@"", @"1,2,3", @"", @"5,3,2,1", @""];
NSLog(@"%@", someArray);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF != ''"];
NSArray *filteredArray = [someArray filteredArrayUsingPredicate:predicate];
NSLog(@"%@", filteredArray);