2011-10-13 64 views
6
-(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

回答

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

應該

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

謝謝你,你是對的 – Gaojian922188

2

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

+0

非常感謝你,你是對的。 – Gaojian922188

相關問題