从NSMutableArray中删除空的值

时间:2012-05-30 10:38:35

标签: iphone objective-c xcode ios5

我有这个数组,我想要删除包含@“”或内部没有任何内容的值。我想保持数组的值和他的新长度。 这怎么可能?

array:{
"24_7" = 1;
"abo_ok" = "";
city = "";
"compte_ok" = "";
country = FR;
"credit_card" = "Mastercard, Visa";
"date_creation" = "2011-11-05 18:01:56";
"debut_abo" = "";
dst = "992.565700179622";
email = "";
"fin_abo" = "";
"h_saturday" = "";
"h_sunday" = "";
"h_week" = "";
handicapped = "Malades assis";
hours = "";
id = 614;
"id_driver" = 614;
"info_compl" = "";
languages = "";
"location_lat" = "48.6823";
"location_long" = "6.17818";
luggage = 0;
luxury = "";
name = "Taxi";
nbvotes = "";
passengers = 4;
query = 8;
score = 9;
"special_trip" = "A\U00e9roport, Colis";
status = 2;
"tel_1" = 0383376537;
"tel_2" = "";
vehicles = "";
votes = "";
}

5 个答案:

答案 0 :(得分:10)

[myMutableArray removeObject: @""];

从数组中删除所有出现的@""Docs for -removeObject here

此外,如果您在评论中提到了一个可变字典

NSArray* keysToGo = [myDictionary allKeysForObject: @""];
[myDictionary removeObjectsForKeys: keysToGo];

答案 1 :(得分:2)

试试这个

NSMutableSet* set = [NSMutableSet setWithArray:array];
[set removeObject:@""];
NSArray *result =[set allObjects];
NSLog(@"%@",result);

答案 2 :(得分:0)

只需遍历数组并删除长度为0的每个字符串:

for(int idx = 0; idx < array.count; idx++) {
    if([[array objectAtIndex:idx] isKindOfClass:[NSString class]]) { //type check
        if(((NSString*)[array objectAtIndex:idx]).length == 0) { //test for string length

            //finally, pull out that string
            [array removeObjectAtIndex:idx]; 

            //because we removed an object, we have to decrement the 
            //counter to avoid skipping the next string
            idx--; 
        }
    }
}

答案 3 :(得分:0)

您可以遍历整个数组并检查每个值的长度。如果值的长度为&lt; = 0,则从数组中删除该对象。

答案 4 :(得分:0)

您可以使用NSMutableArray的removeObjectIdenticalTo:方法,如下所示

[yourMutArray removeObjectIdenticalTo:[NSNull null]];

删除空值。 无需迭代。