Вопрос

I'm trying to insert an object in my dictionary, but first, I want to test if it's a NSMutableDictionary :

if ([[[self.response objectForKey:@"X"] objectAtIndex:0] isKindOfClass:[NSMutableDictionary class]]) {
        [[[self.response objectForKey:@"X"] objectAtIndex:0]setObject:@"Hello" forKey:@"Y"];
    } 

I get this issue :

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
Это было полезно?

Решение 2

Use isMemberOfClass instead of isKindOfClass

Другие советы

You can check for the same selector which is crashing,

if([object respondsToSelector:@selector(setObject:forKey:)]) {
    //If YES, it is NSMutableDictionary
}
else { 
    //Not mutable
}

Hope that helps!

Try this,

if ([[[self.response objectForKey:@"X"] objectAtIndex:0] isKindOfClass:[NSDictionary class]]) {

    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[[self.response objectForKey:@"X"] objectAtIndex:0]];
    [dict setObject:@"Hello" forKey:@"Y"];
    NSMutableArray *xArray = [NSMutableArray arrayWithArray:[self.response objectForKey:@"X"]];
    [xArray replaceObjectAtIndex:0 withObject:dict];
    [self.response setObject:xArray forKey:@"X"];
} 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top