2012-05-30 35 views
2

我有這個數組,我想刪除裏面沒有@「」或者什麼也沒有的值。我想保持數組的值和新的長度。 這怎麼可能?從空的NSMutableArray中刪除值

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

看起來更像NSDictionary! –

+0

是的,我認爲是這樣,但不能刪除空的 – Tidane

+0

在這種情況下,請將您的問題的標題從NSMutableArray更改爲NSMutableDictionary! –

回答

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--; 
     } 
    } 
} 
+0

你可以通過不使用點語法來擺脫醜陋的類型轉換。 '[[array objectAtIndex:idx] length]' – JeremyP

+0

是的,我知道,但由於某種原因,部分我需要爲屬性使用點語法ALWAYS,另一部分我需要爲方法使用Smalltalk消息語法。叫它OCD ... –

+0

哈!我的某些部分需要使用消息傳遞語法,因爲點語法應該在出生時被安樂死(部分原因是這個原因)。 :-) – JeremyP

0

可以通過整個陣列遍歷和檢查每個值。如果長度的值的長度爲< = 0,然後從數組中刪除該對象。

9
[myMutableArray removeObject: @""]; 

移除來自陣列所有出現@""Docs for -removeObject here。有

此外,如果你真的是在評論提到一個可變字典

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

試試這個

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

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

[yourMutArray removeObjectIdenticalTo:[NSNull null]]; 

刪除空值。 無需迭代。