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 = "";
}
4
  • Looks more like NSDictionary! Commented May 30, 2012 at 10:43
  • Yes I think so, but can't arrive to remove the empty ones Commented May 30, 2012 at 11:38
  • In that case please change the title of your question from NSMutableArray to NSMutableDictionary! Commented May 30, 2012 at 11:50
  • Also, this answer will get you through stackoverflow.com/a/9446317/365188 Commented May 30, 2012 at 11:55

5 Answers 5

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];
Sign up to request clarification or add additional context in comments.

Comments

2

Try this

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

Comments

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

3 Comments

You can get rid of the ugly type cast by not using dot syntax. [[array objectAtIndex:idx] length]
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...
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). :-)
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.

Comments

0

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

[yourMutArray removeObjectIdenticalTo:[NSNull null]];

to remove the null values. No need to iterate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.