Strip blank strings from NSMutableArray

Discussion in 'Mac Programming' started by pcwiz, Aug 10, 2009.

  1. pcwiz macrumors member

    Joined:
    May 28, 2008
    #1
    As the title says, whats the best way to strip blank (@"") strings from an MSMutableArray
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    There are probably innumerable ways to do this, but here are a few:
    Code:
    int main(int argc, char *argv[])
    {
    	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    	NSMutableArray *myStrings = [NSMutableArray arrayWithObjects:(id[]){@"Test 1",@"",@"Test 2",@"Test 3",@""} count:5];
    	NSArray *myFilteredArray = [myStrings filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]]; //Non-destructive, myStrings is still intact
    
    	NSLog(@"The original array is: %@",myStrings);
    	NSLog(@"The filtered array is: %@",myFilteredArray);
    	
    	[myStrings removeObject:@""]; //Destructive. myStrings will never be the same again
    	NSLog(@"The altered is: %@",myStrings);
    	[pool drain];
    	return 0;
    }
    
    -Lee
     

Share This Page