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

Gao*_*188 6 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;
}
Run Code Online (Sandbox Code Playgroud)

在我的init方法中,我定义了两个NSMutableArray,并添加到NSMutableDictionary nsmutabledictionary.在我的另一种方法:

[nsmutabledictionary setObject:[NSNumber numberWithLong:100] forKey:@"cu"];
Run Code Online (Sandbox Code Playgroud)

但它崩溃在上面的线: 在此输入图像描述

Aks*_*hay 16

nsmutabledictionary = [[NSDictionary alloc] initWithObjects:listVolumn forKeys:listname];
Run Code Online (Sandbox Code Playgroud)

应该

nsmutabledictionary = [[NSMutableDictionary alloc] initWithObjects:listVolumn forKeys:listname];
Run Code Online (Sandbox Code Playgroud)