0
    NSMutableArray *tmpMutArr = [NSMutableArray arrayWithArray:allObjectsArray];
    NSLog(@"The content of array is%@",tmpMutArr);

    int index;

     for (int i=0;i<[tmpMutArr count];i++) 
     {
     if([[tmpMutArr  objectAtIndex:i] isKindOfClass:[NSDictionary class]])
     {
     NSMutableDictionary *tempDict = [tmpMutArr  objectAtIndex:i];
         if([[tempDict valueForKey:@"Name"] isEqualToString:[NSString stringWithFormat:@"%@", nameString]])
     {
     index = i;

     }
     }
     }

     [tmpMutArr replaceObjectAtIndex:index withObject:[NSDictionary dictionaryWithDictionary:mutDict]];

このコードは、tmpMutArr 内の一致するオブジェクトを置き換えていませんが、代わりに tmpMutArr 内のすべてのオブジェクトを置き換えています。必要なインデックスだけを置き換えるには?

tmpMutArr には置換前のすべてのオブジェクトが含まれていることがわかっているので、インデックスを正しく指定するだけでよいと思います。その方法は?

4

2 に答える 2

5
NSMutableArray *tmpMutArr = [NSMutableArray arrayWithArray:allObjectsArray];
NSLog(@"The content of array is%@",tmpMutArr);

int index;

for (int i=0;i<[tmpMutArr count];i++) 
{
    if([[tmpMutArr  objectAtIndex:i] isKindOfClass:[NSDictionary class]])
    {
        NSMutableDictionary *tempDict = [tmpMutArr  objectAtIndex:i];
        if([[tempDict valueForKey:@"Name"] isEqualToString:nameString])
        {
            index = i;
            break; // << added break
        }
    }
}

[tmpMutArr replaceObjectAtIndex:index withObject:[NSDictionary dictionaryWithDictionary:mutDict]];
于 2012-08-14T18:19:31.020 に答える
0

おそらく、このバージョンを試す必要があります...どのインデックスが必要かを指定していません。最初のものだと思います。

for (int i=0;i<[tmpMutArr count];i++) {
     if([[tmpMutArr  objectAtIndex:i] isKindOfClass:[NSDictionary class]]) {
         NSMutableDictionary *tempDict = [tmpMutArr  objectAtIndex:i];
         if([[tempDict valueForKey:@"Name"] isEqualToString:[NSString stringWithFormat:@"%@", nameString]]) {
             index = i;
             break; // when you find the first one, you should go out from the iteration
         }
     }
 }
于 2012-08-14T18:31:36.400 に答える