这是从NSArray中删除空字符串的正确且最有效的方法吗?

Par*_*att 6 iphone cocoa-touch objective-c nsstring nsarray

这是从NSArray中删除空字符串的正确且最有效的方法吗?

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;
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*iel 6

我会说removeObject是从数组中删除空白对象的最有效方法.NSPredicate应该用于更复杂的结构,所以在这方面,如果你可以使用removeObject,那就太过分了.