Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object

Tags:

iphone

-(id)init{
    if (self==[super init]) {            
        NSMutableArray *listname = [[NSMutableArray alloc]initWithObjects:@"cu",@"al",@"zn",@"au",@"ru",@"fu",@"rb",@"pb",@"wr", nil];            
        NSMutableArray *listVolumn = [NSMutableArray arrayWithCapacity:[listname count]];           
        for (int i=0; i<[listname count]; i++) {
            [listVolumn addObject:[NSNumber numberWithLong:0]];
        }
        nsmutabledictionary = [[NSDictionary alloc] initWithObjects:listVolumn forKeys:listname];
    }
    return self;
}

in my init method,I defined two NSMutableArray,and added to NSMutableDictionary nsmutabledictionary. in my other method:

[nsmutabledictionary setObject:[NSNumber numberWithLong:100] forKey:@"cu"];

but it crash at above line: enter image description here

like image 249
Gaojian922188 Avatar asked Oct 13 '11 03:10

Gaojian922188


1 Answers

nsmutabledictionary = [[NSDictionary alloc] initWithObjects:listVolumn forKeys:listname];

should be

nsmutabledictionary = [[NSMutableDictionary alloc] initWithObjects:listVolumn forKeys:listname];
like image 56
Akshay Avatar answered Sep 19 '22 00:09

Akshay