NSMutableDictionary is being treated as an NSDictionary

Posted by Marc Gelfo on Stack Overflow See other posts from Stack Overflow or by Marc Gelfo
Published on 2010-04-06T07:32:34Z Indexed on 2010/04/06 7:53 UTC
Read the original article Hit count: 568

Hi, I have a simple class with an NSMutableDictionary member variable. However, when I call setObject:forKey I get an error ('mutating method sent to immutable object'). The source of the problem is obvious from the debugger -- my NSMutableDictionary is actually of type NSDictionary.

I must be missing something incredibly simple but can't seem to fix it. Here is the relevant code:

// Model.h
@interface Model : NSObject {
    NSMutableDictionary *piers;
}
@property (nonatomic,retain) NSMutableDictionary *piers;
@end


// Model.m
@implementation Model
@synthesize piers;

-(id) init {
 if (self = [super init]) {
     self.piers = [[NSMutableDictionary alloc] initWithCapacity:2];
        [self createModel];
    }
    return self;
}

-(void) createModel {
 [piers setObject:@"happy" forKey:@"foobar"];  
}
@end

If I put a breakpoint anywhere in the code and investigate self.piers, it is of type NSDictionary. What am I missing so that it is treated as an NSMutableDictionary instead? Thanks!

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about nsdictionary