本文介绍了获取正确的字典索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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 中的匹配对象,因为我想要它to,但替换 tmpMutArr中的所有对象。如何只替换我想要的索引?

This code is not replacing the matching object in tmpMutArr as I want it to, but replaces all objects in tmpMutArr instead. How to replace only the index I want?

我知道tmpMutArr包含替换前的所有对象,所以我只需要正确指定索引。怎么做?

I know that tmpMutArr containing all objects before the replacement, so I just need to specify the index correctly I think. How to do so?

推荐答案

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

这篇关于获取正确的字典索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 05:07