开发者

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

开发者 https://www.devze.com 2023-04-13 03:58 出处:网络
-(id)init{ if (self==[super init]) { NSMutableArray *listname = [[NSMutableArray alloc]initWithObjects:@\"cu\",@\"al\",@\"zn\",@\"au\",@\"ru\",@\"fu\",@\"rb\",@\"pb\",@\"wr\", nil];
-(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:

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消