从NSDictionary数组中删除重复的键值?

时间:2016-07-19 12:52:38

标签: ios objective-c nsdictionary

我需要逐个比较两个数组值,如果有任何值彼此匹配,我需要更新该值并将其替换为主数组。

NSMutableArray 的数组 NSDictionary

NSArray 有一个 NSString 对象数组。

我的代码是

NSMutableDictionary *dicOne = @{@"eveId":@"1",@"imgArray":@[@"one",@"two"]};
    NSMutableDictionary *dicTwo = @{@"eveId":@"2",@"imgArray":@[@"Three",@"Four"]};
    NSMutableArray *tempArray = [NSMutableArray array];
    [tempArray addObject:dicOne];
    [tempArray addObject:dicTwo];

    NSArray *eventArray = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];
    NSString *fileName = @"Five";
    for (int jj = 0; jj<[eventArray count]; jj++) {
        NSString *topArrayEventId = [NSString stringWithFormat:@"%@",[eventArray objectAtIndex:jj]];

        for (int kk = 0; kk<[tempArray count]; kk++) {
            NSMutableDictionary *curentObj = [[tempArray objectAtIndex:kk]mutableCopy];
            NSString *innerArrayEventId = [NSString stringWithFormat:@"%@",[curentObj valueForKey:@"eveId"]];

            if ([innerArrayEventId isEqualToString:topArrayEventId]) {
                NSLog(@"update inner array and replace main array");

                NSMutableArray *imgArray = [NSMutableArray array];
                imgArray = [[curentObj objectForKey:@"imgArray"]mutableCopy];
                [imgArray addObject:fileName];
                [curentObj setObject:[imgArray mutableCopy] forKey:@"imgArray"];
                [tempArray replaceObjectAtIndex:kk withObject:[curentObj mutableCopy]];
            }else{
                NSLog(@"add new object to main array");
                NSMutableDictionary*  storeData = [NSMutableDictionary dictionary];
                [storeData setObject:topArrayEventId forKey:@"eveId"];
                NSMutableArray *imgArray = [NSMutableArray array];
                [imgArray addObject:fileName];
                [storeData setObject:imgArray forKey:@"imgArray"];
                [tempArray addObject:storeData];
            }

        }
    }
    NSLog(@"final updation------->%@",tempArray);

我想要输出如下

(
{
    eveId = 1;
    imgArray =     (
                    one,
                    two,
                    five
                    );
},
{
    eveId = 2;
    imgArray =     (
                    Three,
                    Four,
                    five
                    );
}
{
    eveId = 3;
    imgArray =     (
                    five

                    );
}
)

但我没有得到那个代码..给出了错误的输出。我有内环问题。

输出错误

(
{
    eveId = 1;
    imgArray =     (
        one,
        two,
        Five
    );
},
{
    eveId = 2;
    imgArray =     (
        Three,
        Four,
        Five
    );
},
{
    eveId = 1;
    imgArray =     (
        Five,
        Five
    );
},
{
    eveId = 2;
    imgArray =     (
        Five,
        Five
    );
},
{
    eveId = 2;
    imgArray =     (
        Five,
        Five
    );
},
{
    eveId = 3;
    imgArray =     (
        Five,
        Five
    );
},
{
    eveId = 3;
    imgArray =     (
        Five,
        Five
    );
},
{
    eveId = 3;
    imgArray =     (
        Five,
        Five
    );
},
{
    eveId = 3;
    imgArray =     (
        Five,
        Five
    );
},
{
    eveId = 3;
    imgArray =     (
        Five,
        Five
    );
}
)

3 个答案:

答案 0 :(得分:1)

用于实现此目的的谓词方法

    NSMutableDictionary *dicOne = @{@"eveId":@"1",@"imgArray":@[@"one",@"two"]};
NSMutableDictionary *dicTwo = @{@"eveId":@"2",@"imgArray":@[@"Three",@"Four"]};
NSMutableArray *tempArray = [NSMutableArray array];
[tempArray addObject:dicOne];
[tempArray addObject:dicTwo];

NSArray *eventArray = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];
NSString *fileName = @"Five";


for (int jj = 0; jj<[eventArray count]; jj++) {

    NSArray *resultArray = [tempArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.eveId = %@", [eventArray objectAtIndex:jj]]];
    NSString *topArrayEventId = [NSString stringWithFormat:@"%@",[eventArray objectAtIndex:jj]];


    if([resultArray count]>0) {
    NSMutableDictionary *curentObj;
    NSString *innerArrayEventId = [NSString stringWithFormat:@"%@",[[resultArray objectAtIndex:0] valueForKey:@"eveId"]];
    if ([innerArrayEventId isEqualToString:topArrayEventId]) {
    curentObj= [[resultArray objectAtIndex:0]mutableCopy];
    NSMutableArray *imgArray = [NSMutableArray array];
    imgArray = [[[resultArray objectAtIndex:0] objectForKey:@"imgArray"]mutableCopy];
    [imgArray addObject:fileName];
    [curentObj setObject:[imgArray mutableCopy] forKey:@"imgArray"];
    [tempArray replaceObjectAtIndex:jj withObject:[curentObj mutableCopy]];
    }
    }
    else {
        NSMutableDictionary*  storeData = [NSMutableDictionary dictionary];
        [storeData setObject:topArrayEventId forKey:@"eveId"];
        NSMutableArray *imgArray = [NSMutableArray array];
        [imgArray addObject:fileName];
        [storeData setObject:imgArray forKey:@"imgArray"];
        [tempArray addObject:storeData];
    }

}
NSLog(@"final updation------->%@",tempArray);

 OutPut : 
final updation------->(
        {
        eveId = 1;
        imgArray =         (
            one,
            two,
            Five
        );
    },
        {
        eveId = 2;
        imgArray =         (
            Three,
            Four,
            Five
        );
    },
        {
        eveId = 3;
        imgArray =         (
            Five
        );
    }
)

答案 1 :(得分:1)

考虑将NSMutableArray替换为使用NSMutableSet

NSMutableArray *imgArray = [NSMutableArray array];

NSMutableSet *imgSet = [NSMutableSet set];

https://developer.apple.com/reference/foundation/nsmutableset

答案 2 :(得分:0)

更改此行

for (int kk = 0; kk<[tempArray count]; kk++)

for (int kk = jj; kk<[tempArray count]; kk++)

现在运行代码,现在有效吗?