NSMutableDictionary添加數據時遇到的問題

//根據需求,我需要在網絡請求後在數據源裏面添加另外的一個參數
[manager GET:@"http://mobile.pinlehuo.com/api.php?m=Wallet&a=bindcard&user_id=1101" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
     for (NSMutableDictionary *dic in responseObject[@"data"]) {
          [dic setValue:@"YES" forKey:@"checked"];
          [dataArray addObject:dic];
      }
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      NSLog(@"%@", error);
  }];
 //我按照上面的方法直接在遍歷後往字典裏面添加參數 會報這個錯誤
 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'在網上查了一下,說是因爲2個字典的內存地址不一樣照成的

//下面我又初始化了一個字典,根據現有的字典創建字典 成功了
 NSMutableDictionary *mutableItem;
  for (NSDictionary *dic in responseObject[@"data"]) {
    // NSLog(@"%@", dic);
  mutableItem = [NSMutableDictionary dictionaryWithDictionary:dic];
 [mutableItem setValue:@"YES" forKey:@"checked"];

 [dataArray addObject:mutableItem];
 }





//下面附上一些NSDictionary內容
來自http://blog.csdn.net/ms2146/article/details/8656787
 //創建字典  
NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];  
 NSLog(@"dic1 :%@", dic1);  


 //創建多個字典  
 NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:  
                              @"value1", @"key1",  
                              @"value2", @"key2",  
                              @"value3", @"key3",  
                              @"value4", @"key4",  
                              nil];  
 NSLog(@"dic2 :%@", dic2);  


//根據現有的字典創建字典  
NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic2];  
NSLog(@"dic3 :%@", dic3);  


//根據key獲取value  
NSLog(@"key3 value :%@", [dic3 objectForKey:@"key3"]);  

//獲取字典數量  
NSLog(@"dic count :%d", dic3.count);  

//所有的鍵集合  
NSArray *keys = [dic3 allKeys];  
NSLog(@"keys :%@", keys);  

//所有值集合  
NSArray *values = [dic3 allValues];  
NSLog(@"values :%@", values);  



NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:  
                                           @"mvalue1", @"mkey1",  
                                           @"mvalue2", @"mkey2", nil];  
//添加現有的字典數據  
[mutableDic addEntriesFromDictionary:dic3];  
NSLog(@"mutableDic :%@",mutableDic);  

//添加新的鍵值對象  
[mutableDic setValue:@"set1" forKey:@"setKey1"];  
NSLog(@"set value for key :%@",mutableDic);  

//以新的字典數據覆蓋舊的字典數據  
 [mutableDic setDictionary:dic2];  
 NSLog(@" set dictionary :%@",mutableDic);  

//根據key刪除value  
[mutableDic removeObjectForKey:@"key1"];  
NSLog(@"removeForkey :%@",mutableDic);  

//快速遍歷  
for(id key in mutableDic) {  
NSLog(@"key :%@  value :%@", key, [mutableDic objectForKey:key]);  
   }  

//枚舉遍歷  
NSEnumerator *enumerator = [mutableDic keyEnumerator];  
id key = [enumerator nextObject];  
 while (key) {  
 NSLog(@"enumerator :%@", [mutableDic objectForKey:key]);  
key = [enumerator nextObject];  
 }  

 //根據key數組刪除元素  
 [mutableDic removeObjectsForKeys:keys];  
 NSLog(@"removeObjectsForKeys :%@",mutableDic);

 //刪除所有元素         
[mutableDic removeAllObjects];  

NSLog(@"remove all :%@", mutableDic);  
發佈了30 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章