[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object
-(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]]; 开发者_JAVA技巧
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:
nsmutabledictionary = [[NSDictionary alloc] initWithObjects:listVolumn forKeys:listname];
should be
nsmutabledictionary = [[NSMutableDictionary alloc] initWithObjects:listVolumn forKeys:listname];
In your code I see that nsmutabledictionary = [[NSDictionary alloc] initWithObjects:listVolumn forKeys:listname];
that line you declared it to be NSDictionary while you intented it to be NSMutableDictionary
精彩评论