2015-11-05 48 views
0

我有一个NSMutableArray下面这样如何从目标C中的另一个值替换NSDictionary值?

(
    { 
    Realimg = "<UIImage: 0x13951b130>, {800, 600}"; 
    "bid_accepted" = 1; 
    "bid_amount" = 100; 
    "bid_amount_num" = 100; 
    "bid_cur_name" = USD; 
    "bid_cur_symb" = $; 
    "bid_currencycode" = 4; 
    "bid_date" = "05 Nov 2015"; 
    "bid_msg" = testing; 
    "bid_user_id" = 2; 
    "nego_id" = 612; 
    "pas_count" = 5; 
    "ride_address" = "Sample Address"; 
    "ride_cover_img" = "uploadfiles/UploadUserImages/2/mobile/21426739600s-end-horton-plains.jpg"; 
    "ride_id" = 149; 
    "ride_name" = "Ride to the World's End"; 
    "ride_type_id" = 0; 
    "ride_type_name" = Travel; 
    "ride_user_fname" = Nik; 
    "ride_user_id" = 2; 
    "ride_user_lname" = Mike; 
    } 

) 

我想要做的是,取代 “bid_currencycode”= 4;由一个不同的值。我想用5替换4。我怎么能做到这一点?请有人帮助我。

谢谢

+1

你可以发布你正在使用创建数组或做更改代码更换 –

回答

3

您可以使用下面的代码做同样的:

// Getting the dictionary from your array and making it to a mutable copy 
NSMutableDictionary *dict = [yourArray[index] mutableCopy]; 

// Changing the specified key 
[dict setObject:@(5) forKey:@"bid_currencycode"]; 

// Replacing the current dictionary with modified one 
[yourArray replaceObjectAtIndex:index withObject:dict]; 

这里:

  • yourArrayNSMutableArray
  • index是有效的索引(索引的对象,你需要改变它的值)
1

请参阅下面的编码

NSMutableDictionary *dict =[[NSMutableDictionary alloc]init]; 

//your array 

dic =[yourArray objectAtIndex:0]; 

[dict removeObjectForKey:@"bid_currencycode"]; 

[dict setObject:@“5” forKey:@"bid_currencycode"]; 

[yourArray replaceObjectAtIndex:0 withObject:dict]; 

0(yourArray索引号),您可以根据您的数组索引号

相关问题