3

I have this array and I want to delete the values that have @"" or nothing inside. I want to keep the array with the values and with his new length. How is this possible?

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 = "";
}
Tidane
  • 694
  • 1
  • 10
  • 22

5 Answers5

10
[myMutableArray removeObject: @""];

Removes all occurrences of @"" from the array. Docs for -removeObject here.

Also if you really have a mutable dictionary as alluded to in the comments

NSArray* keysToGo = [myDictionary allKeysForObject: @""];
[myDictionary removeObjectsForKeys: keysToGo];
JeremyP
  • 84,577
  • 15
  • 123
  • 161
2

Try this

NSMutableSet* set = [NSMutableSet setWithArray:array];
[set removeObject:@""];
NSArray *result =[set allObjects];
NSLog(@"%@",result);
Hector
  • 3,909
  • 2
  • 30
  • 46
0

Just loop through the array and remove each string with a length of 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--; 
        }
    }
}
eric.mitchell
  • 8,817
  • 12
  • 54
  • 92
  • You can get rid of the ugly type cast by not using dot syntax. `[[array objectAtIndex:idx] length]` – JeremyP May 30 '12 at 10:58
  • Yeah, I know, but for some reason a part of me NEEDS to use dot syntax for properties ALWAYS, and another part of me needs to use Smalltalk messaging syntax for methods ONLY... call it OCD... – eric.mitchell May 30 '12 at 11:01
  • Ha! Some part of me NEEDS to use messaging syntax always because dot syntax should have been euthanised at birth (partly for this very reason). :-) – JeremyP May 30 '12 at 11:04
0

You can traverse through entire array and check the length for each value.If the length of value is <=0, then remove that object from array.

Nuzhat Zari
  • 3,398
  • 2
  • 24
  • 36
0

You can use NSMutableArray's removeObjectIdenticalTo: method, as follows

[yourMutArray removeObjectIdenticalTo:[NSNull null]];

to remove the null values. No need to iterate.

Ash
  • 5,525
  • 1
  • 40
  • 34