Ace Munim
Ace Munim

Reputation: 325

Deleting <null> inside an NSMutableDictionary inside an NSMutableArray

I been searching for a while now on how to remove nil, null, values here and into google are tried all what i saw but im always having an exception which says selector not imcompatible. This is my Array and NSMutableArray with an NSMutableDictionary

Array Value = (
        {
           "sub_desc" = "sub cat description of Laptop";
           "sub_name" = Laptop;
        },
        {
           "sub_desc" = "sub cat description of Printers";
           "sub_name" = Printers;
        },
           "<null>",
           "<null>",
           "<null>",
           "<null>"
)

and im trying to remove the values, any ideas?

my segue goes like this

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"MySegue"]) {

        ChildTableViewController *myNextPage = [segue destinationViewController];
        if ([myNextPage isKindOfClass:[ChildTableViewController class]]){  
            NSString *key = [[[parsedData listPopulated]objectAtIndex:self.tableView.indexPathForSelectedRow.row]valueForKey:@"name"];
            myNextPage.childData = [[parsedData childPopulated]valueForKey:key]; 
        }
    }
}

and childData is an NSMutableArray in my Child View Controller and also childPopulated is an NSMutableArray where Im inserting NSMutableDictionary.

Upvotes: 4

Views: 214

Answers (2)

Juan Catalan
Juan Catalan

Reputation: 2309

This code should o it:

NSMutableIndexSet *indexSet;
for (NSUinteger i = 0; i < [array count]; i++) {
    if (![array[i] isKindOfClass:[NSNull class]]) {
        [indexSet addIndex:i]
    }
}
array = [array objectsAtIndexes:indexSet]

If you want to remove objects that are the string null then:

NSMutableIndexSet *indexSet;
for (NSUinteger i = 0; i < [array count]; i++) {
    if ([array[i] isKindOfClass:[NSString class]] && [array[i] isEqualToString:@"<null>") {
        [indexSet addIndex:i]
    }
}
[array removeObjectsAtIndexes:indexSet]

Upvotes: 2

trojanfoe
trojanfoe

Reputation: 122391

Use [NSMutableArray removeObject:]:

Removes all occurrences in the array of a given object.

NSMutableArray *array = ...;
[array removeObject:@"<null>"];

Note: your array contains a mix of dictionary and strings objects; not just dictionary objects.

Upvotes: 4

Related Questions