iOS 数组相同元素的提取,合并

2018-07-31  本文已影响0人  xiaoUUU

开发中,我们整理数据时可能会碰到这些情况,比如:有n多条全国店铺信息,我们需要按城市来整理店铺信息(本来这是后台的事,不过难免有特殊情况)。


- (NSMutableDictionary *)getNewDic {

    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];

    NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"2",@"3",@"3",@"3", nil];

    NSMutableArray *array1 = [NSMutableArray array];

    __block NSMutableArray *array2 = [NSMutableArray array];

    [array enumerateObjectsUsingBlock:^(NSString * obj, NSUInteger idx, BOOL * _Nonnull stop) {

        NSMutableArray *eachCitySiteArr = [NSMutableArray array];

        [array enumerateObjectsUsingBlock:^(id  _Nonnull obj1, NSUInteger idx1, BOOL * _Nonnull stop1) {

            if([array[idx1] isEqualToString:array[idx]]) {

                [eachCitySiteArr addObject:array[idx1]];

            }

        }];

        [dic setObject:eachCitySiteArr forKey:array[idx]];

        if([array[idx] isEqualToString:@"2"]) {

            array2 = eachCitySiteArr;

        }

        if(![array1 containsObject:array[idx]]) {

            [array1 addObject:array[idx]];

        }

    }];



//    NSLog(@"dic :%@",dic);

    NSLog(@"array1 :%@",array1);

    NSLog(@"array2 :%@",array2);

    return dic;

}

Android的方式

上一篇下一篇

猜你喜欢

热点阅读