在Objective-C iOS中为相同的值组合对象数组

在Objective-C iOS中为相同的值组合对象数组,ios,objective-c,iphone,compare,Ios,Objective C,Iphone,Compare,我有一个自定义对象名数组finalBarListArray,带有实体BarCodeSKULists(请参阅附件1),其中包含sales\u sub\u category\u name(字符串类型),count(int类型)和另一个自定义对象名数组arrayBarCodeSKUList(BarCodeSKUList)(参考附件2/3/4) 请参见以下屏幕截图: 现在我需要创建一个数组,并检查是否会有相同的sales\u sub\u category\u name值,然后需要将它们的arra

我有一个自定义对象名数组
finalBarListArray
,带有实体
BarCodeSKULists
(请参阅附件1),其中包含
sales\u sub\u category\u name(字符串类型)
count(int类型)
和另一个自定义对象名数组
arrayBarCodeSKUList(BarCodeSKUList)
(参考附件2/3/4)

请参见以下屏幕截图:

现在我需要创建一个数组,并检查是否会有相同的
sales\u sub\u category\u name
值,然后需要将它们的
arrayBarCodeSKUList
添加到一个数组中


就像上面的数组
finalBarListArray
一样,有三个元素的
sales\u sub\u category\u名称为'envious 17'、'envious 15'和'envious 15'
,所以我需要将第二个和第三个元素合并为一个,作为数组的第二个索引。这意味着输出应该只有两个元素,而第一个元素应该有两个(
count=1
&one
arrayBarCodeSKUList
),第二个元素应该有(
count=2
&two
arrayBarCodeSKUList
)。

使用以下方法合并具有相同
sales\u sub\u category\u名称的自定义对象数组

- (NSArray *)mergeDuplicate {

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

    // Use sales_sub_category_name value as a key for the dictioanry.

    [finalBarListArray enumerateObjectsUsingBlock:^(BarCodeSKULists * _Nonnull object, NSUInteger idx, BOOL * _Nonnull stop) {

        id existingItem = [mergedDictionary valueForKey:object.sales_sub_category_name];
        if (existingItem) {
            // If object exist then check that type is NSMutableArray or not
            if ([existingItem isKindOfClass:[NSMutableArray class]]) {

                // If yes then append with existing array
                [existingItem addObject:object];
                mergedDictionary[object.sales_sub_category_name] = existingItem;
            } else if ([existingItem isKindOfClass:[BarCodeSKULists class]]) {
                // Else if the object is `BarCodeSKULists ` class then create array and added previous item and current item into one array
                NSMutableArray *itemList = [NSMutableArray arrayWithObjects:existingItem, object, nil];
                mergedDictionary[object.sales_sub_category_name] = itemList;
            }
        } else {
            // If it is first time then add it to the dictionary
            mergedDictionary[object.sales_sub_category_name] = object;
        }

    }];

    NSLog(@"%@", mergedDictionary.allValues);
    return mergedDictionary.allValues;
}
合并词典。所有值
将给出预期的项目数组

更新:

根据讨论

- (NSArray *)mergeDuplicate:(NSMutableArray *) list{

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

    // Use sales_sub_category_name value as a key for the dictioanry.

    [list enumerateObjectsUsingBlock:^(BarCodeSKUList * _Nonnull object, NSUInteger idx, BOOL * _Nonnull stop) {

        BarCodeSKUList *existingItem = [mergedDictionary valueForKey:object.sales_sub_category_name];
        if (existingItem) {
            [existingItem.arrayBarCodeSKUList addObjectsFromArray:object.arrayBarCodeSKUList];
            mergedDictionary[object.sales_sub_category_name] = existingItem;
        } else {
            // If it is first time then add it to the dictionary
            mergedDictionary[object.sales_sub_category_name] = object;
        }

    }];


    return mergedDictionary.allValues;
}

新数组中元素的对象类型是什么?仍然是“BarCodeSKUList”?第二个元素的含义是什么?我们有两个ArrayBarcodeskList?BarCodeSKUList是我的模态类名,在这个实体中,我有另一个模态类名BarCodeSKUList和一个数组“arrayBarCodeSKUList”。
(count=1&one arrayBarCodeSKUList)…(count=2&two arrayBarCodeSKUList)
您的意思是
arrayBarCodeSKList
将是count=1和count=2,没有?未测试,但是:?@LarmeThanks用于您的响应。我的意思是最终数组应该有两个元素,其中一个是sales\u sub\u category\u name=“envious 17”,Count=1&arrayBarCodeSKUList=1,另一个是sales\u sub\u category\u name=“ENVY 15”,Count=2&BarCodeSKUList对象的两个arrayBarCodeSKUList数组。非常感谢您的时间@Subramanian,我已经测试了上述解决方案,但这是以键/值对形式给出的结果(由于字典原因)而不是自定义对象的数组格式。如何实现这一点?原因是我需要将自定义数组对象中的数据传递到表节及其相同子类别名称的相关行。谢谢!从字典中可以获得值数组
mergedDictionary.allValues
。在代码之后,我已经提到了这一点also@AnandGautamN现在,我已经将函数返回类型更改为
NSArray
好的,我明白了。但是,在这里,您已经根据键“sales\u sub\u category\u name”添加了一个完整的BarCodeSKUList类对象。但是作为一个输出,我只想添加arrayBarCodeSKUList(BarCodeSKUList类的数组)感谢您的回答,它也在工作。+1:)
- (NSArray *)mergeObject{

    NSMutableDictionary *dic = [NSMutableDictionary new];

    [_originArray enumerateObjectsUsingBlock:^(BarCodeSKULists*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        id item = [dic objectForKey:obj.sales_sub_category_name];

        if (item) {
            BarCodeSKULists *list = (BarCodeSKULists *)item;
            [list.arrayBarCodeSKUList addObject:obj.BarCodeSKUList];
            list.count = (int)list.arrayBarCodeSKUList.count;
            dic[obj.sales_sub_category_name] = list;
        }
        else{
            dic[obj.sales_sub_category_name] = obj;
        }
    }];

    return dic.allValues;
}