这是从 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;
}
Is this a right and most efficient way to remove blank string from 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说,removeObject 是从数组中删除空白对象的最有效方法。 NSPredicate 应该用于更复杂的结构,因此在这方面,考虑到您可以使用removeObject,它是一种矫枉过正的做法。
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.