3

我有这个数组,我想删除里面有 @"" 或没有的值。我想保持数组的值和他的新长度。这怎么可能?

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 = "";
}
4

5 回答 5

10
[myMutableArray removeObject: @""];

从数组 中删除所有出现的。-removeObject 的文档在这里@""

另外,如果您真的有评论中提到的可变字典

NSArray* keysToGo = [myDictionary allKeysForObject: @""];
[myDictionary removeObjectsForKeys: keysToGo];
于 2012-05-30T10:53:38.143 回答
2

尝试这个

NSMutableSet* set = [NSMutableSet setWithArray:array];
[set removeObject:@""];
NSArray *result =[set allObjects];
NSLog(@"%@",result);
于 2012-05-30T10:58:45.940 回答
0

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

于 2012-05-30T10:46:30.950 回答
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--; 
        }
    }
}
于 2012-05-30T10:42:09.103 回答
0

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

[yourMutArray removeObjectIdenticalTo:[NSNull null]];

删除空值。无需迭代。

于 2016-06-20T07:34:05.493 回答