如何用 Objective C 中的另一个值替换 NSDictionary 值?

How to replace the NSDictionary value from another value in Objective C?

我下面有一个NSMutableArray这样的

(
   {
    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; by a different value.I 想用 5 替换 4。我该怎么做?请有人帮助我。

谢谢

您可以使用以下代码来执行相同的操作:

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

这里:

  • yourArray 是一个 NSMutableArray
  • index 是一个有效的索引(对象的索引,您需要更改该值)

参考以下编码

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 is (yourArray index number) 你可以根据你的数组索引号改变