[__NSCFDictionary setObject:forKey:]:发送给不可变对象的变异方法 [英] [__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object

查看:36
本文介绍了[__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.在我的另一种方法中:

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];

应该是

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

这篇关于[__NSCFDictionary setObject:forKey:]:发送给不可变对象的变异方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆