dugla
dugla

Reputation: 12954

What is the quickest way to remove empty NSStrings from an NSMutableArray?

I have an NSMutableArray with NSStrings for items. Some are blank. What is the quickest way to find and remove the blanks without having to resort to looping, comparing to @"", etc.

Thanks,
Doug

Upvotes: 1

Views: 51

Answers (3)

Joe
Joe

Reputation: 2987

NSMutableArray *test = [[NSMutableArray alloc]initWithObjects:@"",@"",@"test",@"test1", nil];
[test removeObject:@""];

Upvotes: 3

Midhun MP
Midhun MP

Reputation: 107121

You can use NSPredicate for doing this.

NSArray *myFilteredArray = [yourArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]];

Upvotes: 0

Ed Marty
Ed Marty

Reputation: 39690

[a filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
    [evaluatedObject length] != 0;
}]];

Upvotes: 0

Related Questions