从NSArray中删除空NSStrings的最简单方法是什么?
问题描述:
在PHP中,它是一行代码:
In PHP it's one line of code:
$array_without_empty_strs = array_filter($array_with_empty_strs);
目标C当量是什么?
更新 - 添加以下测试代码以说明Nikolai Ruhe解决方案的使用:
UPDATE - Added the following test code to illustrate the use of Nikolai Ruhe's solution:
// SOLUTION Test Code
NSMutableArray *myArray = [[NSMutableArray alloc] init ];
[myArray addObject:[NSNumber numberWithInt:5]];
[myArray addObject:@""];
[myArray addObject:@"test"];
NSLog(@"%@", myArray);
[myArray removeObject:@""];
NSLog(@"%@", myArray);
// SOLUTION Test Code Output
2012-07-12 08:18:16.271 Calculator[1527:f803] (
5,
"",
test
)
2012-07-12 08:18:16.273 Calculator[1527:f803] (
5,
test
)
答
这更简单:
[mutableArrayOfStrings removeObject:@""];
如果你的数组不可变,你必须创建一个 mutableCopy
之前。
If your array is not mutable you have to create a mutableCopy
before.
removeObject:
从数组中删除所有对象从返回YES是等于:
。
removeObject:
removes all objects from an array that return YES from isEqual:
.