-2

我有如下数组:

(
  {
    id = 4;
    name = "AAAA";
  },

  {
    id = 5;
    name = "BBBB";
   },
 {  
    id = 9;
    name = "BBBB";
   }
)

如何更改 id=5 的名称?

4

2 回答 2

2

尝试这样的事情。

假设您的数组被调用array并且 yourNewNameString 是您的新值name

for(NSMutableDictionary *dict in array){
    if([[dict objectForKey:@"id"]integerValue]==5){
        [dict setObject:yourNewNameString forKey@"name"];
    }
}

编辑 这是假设你用NSMutableDictionarys(不仅仅是NSDictionarys)初始化你的数组

于 2013-07-26T05:18:41.807 回答
1
    suppose Array = (
      {
        id = 4;
        name = "AAAA";
      },

      {
        id = 5;
        name = "BBBB";
       },
     {  
        id = 9;
        name = "BBBB";
       }
    )

    //Now to change the name where the id=5

    NSMutableDictionary *dict = [array objectAtIndex: 1];
    [dict setObject :@"hari kishan" forKey:@"name'];//this will change the name where id = 5
//Or if want to make dynamic

-(void)changeNameForId:(NSString*)id
{
for(NSMutableDictionary *dictionary in array)
{
    if([[dictionary objectForKey:@"id" isEqualToString :id]){
        [dictionary setObject:@"hari kishan" forKey@"name"]
    }
}

}

于 2013-07-26T05:21:46.627 回答