[__NSCFDictionary setObject:forKey:]:发送到不可变对象的变异方法

问题描述:

-(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; 
} 

在我的init方法中,我定义了两个NSMutableArray,并将其添加到NSMutableDictionary nsmutabledictionary中。在我的另一种方法 :[__NSCFDictionary setObject:forKey:]:发送到不可变对象的变异方法

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

但它坠毁在上述行: enter image description here

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

应该

nsmutabledictionary = [[NSMutableDictionary alloc] initWithObjects:listVolumn forKeys:listname]; 
+0

谢谢你,你是对的 – Gaojian922188

在你的代码中,我看到nsmutabledictionary = [[NSDictionary alloc] initWithObjects:listVolumn forKeys:listname]; 那行,你声明它是NSDictionary而你的意图是NSMutableDictionary

+0

非常感谢你,你是对的。 – Gaojian922188