开发者

Is this a right and most efficient way to remove blank string from NSArray?

开发者 https://www.devze.com 2023-03-29 10:47 出处:网络
Is this a right and most efficient way to remove blank string from NSArray? int main(int argc, char *argv[])

Is this a right and most efficient way to remove blank string from NSArray?

int main(int argc, char *argv[])
{
    NSA开发者_JAVA百科utoreleasePool * 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;
}


I would say removeObject is the most efficient way of removing the blank objects from your array. NSPredicate should be used for more complex structures, so in that respect it's an overkill given you could use removeObject.

0

精彩评论

暂无评论...
验证码 换一张
取 消