合并阵列IOS中的相同对象

时间:2013-11-26 06:12:24

标签: ios arrays merge

我有一个包含这些对象的数组:

(
    {
        aOptns =             (
        );
        fCustomDscnt = 0;
        fPrcntDscnt = 0;
        fPrice = 0;
        fQty = 1;
        iItemDayPriceId = 143;
        iShiftId = 1;
        sItemName = "";
        sModifier = "";
    },
    {
        aOptns =             (
        );
        fCustomDscnt = 0;
        fPrcntDscnt = 0;
        fPrice = 0;
        fQty = 1;
        iItemDayPriceId = 143;
        iShiftId = 1;
        sItemName = "";
        sModifier = "";
    },
    {
        aOptns =             (
        );
        fCustomDscnt = 0;
        fPrcntDscnt = 0;
        fPrice = 0;
        fQty = 1;
        iItemDayPriceId = 143;
        iShiftId = 1;
        sItemName = "";
        sModifier = "";
    },
    {
        aOptns =             (
        );
        fCustomDscnt = 0;
        fPrcntDscnt = 0;
        fPrice = 0;
        fQty = 1;
        iItemDayPriceId = 112;
        iShiftId = 1;
        sItemName = "";
        sModifier = "";
    }
)

如果对象相同,我需要合并数组的内容,并以这样的方式修改该数组中的对象:

(
        {
            aOptns =             (
            );
            fCustomDscnt = 0;
            fPrcntDscnt = 0;
            fPrice = 0;
            fQty = 3;
            iItemDayPriceId = 143;
            iShiftId = 1;
            sItemName = "";
            sModifier = "";
        },

        {
            aOptns =             (
            );
            fCustomDscnt = 0;
            fPrcntDscnt = 0;
            fPrice = 0;
            fQty = 1;
            iItemDayPriceId = 112;
            iShiftId = 1;
            sItemName = "";
            sModifier = "";
        }
    )

如您所见,iItemDayPriceId = 143的对象条目仅在fQty = 3时变为1。

我尝试过使用此处的代码:How to Find Duplicate Values in Arrays?

但它一次只比较2个物体。

2 个答案:

答案 0 :(得分:0)

编辑:哎呀,我错过了你的计数要求。

编辑2:事实上你想改变给定的数组,而不是构建一个新数组。

取决于你的意思是不同但是因为看起来你的意思是比较内容而不是引用,你应该建立一个新的数组,这样如果比较为真你增加计数,否则你添加对象。

改变现有数组

免责声明:这可能不是我最好的工作。

如果有人可以建议如何改进这一点,我将不胜感激,因为我非常确定这不是最好的方法,甚至可能无法工作(目前无法访问Obj-C编译器)。

Foo objectA = [Foo new];
objectA.Bar = @"Bar";
Foo objectB = [Foo new];
objectB.Bar = @"Bar";
Foo objectC = [Foo new];
objectC.Bar = @"Not Bar";

NSMutableArray *array = @[objectA, objectB, objectC];

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

for (Foo *foo in [array copy])
{
    bool found = false;

    for (Foo *key in countDictionary)
    {
        if ([key isEqualTo:foo])
        {
            NSNumber *currentCount = [countDictionary objectForKey:key];
            int currentIntCount = [currentCount intValue];
            currentIntCount++;
            [countDictionary setObject:[NSNumber numberWithInt:currentIntCount] forKey:key];

            [array removeObject:key];

            found = true;

            break;
        }

        if (!found)
            [countDictionary setObject:[NSNumber numberWithInt:1] forKey:foo];
    }
}

for (Foo *realFoo in array)
{
    NSNumber *countNumber = [countDictionary objectForKey:realFoo];
    int countInt = [countNumber intValue];

    realFoo.Count = countInt;
}

构建新数组

Foo objectA = [Foo new];
objectA.Bar = @"Bar";
Foo objectB = [Foo new];
objectB.Bar = @"Bar";

NSMutableArray *array = @[objectA, objectB];
NSMutableArray *distinct = [[NSMutableArray alloc] init];

for (Foo *f in array)
{
    bool found = false;
    for (Foo *d in distinct)
    {
        if ([d isEqualTo:f])
        {               
            found = true;
            d.Count++;
        }
        break;
    }

    if (!found)
        [distinct addObject:f];
}

当然,您必须为Foo定义一个相等比较器,在本例中为

-(bool) isEqualTo:(Foo *)nFoo
{
    bool isEqual = false;

    if ([self.Bar isEqualToString:nFoo.Bar])
         isEqual = true;

    return isEqual;
}

暂时没有处理目标C我应该说可能有一些最佳实践来实现相等比较,而不是像这个例子那样完全滚动你自己

答案 1 :(得分:0)

使用以下代码:

 NSMutableArray* myArray =
 [[NSMutableArray alloc]initWithObjects:
 @"red",@"blue",@"red",@"green",@"yellow", @"33", @"33",@"red", @"123", @"123",nil];

 NSOrderedSet *mySet = [[NSOrderedSet alloc] initWithArray:myArray];
 myArray = [[NSMutableArray alloc] initWithArray:[mySet array]];

 NSLog(@"%@",myArray);