0

我正在处理 JSON NSMutableArray,字段存储在对象中,然后存储在NSMutableArray. 现在,由于数据库的设计方式,最顶层的字段(我称之为类别)会重复。我正在尝试合并重复项,例如一个名为 play 的对象(playname、playdate、playCharacters)。PlayCharacters 也是一个 NSmutableArray,所以一个 playName 可以有一个或多个 playcharacter

所有的播放对象都存储在一个 final 中NSMutableArray;播放列表。我的代码消除了重复项,但我不只是想删除重复项,我想合并它们

例如 [网球 - (日期: 星期四打), (playCharacters: moses, john, ken)

[网球-(日期:周五打球),(打球角色:leo,bill)]

我想要的是

[网球-(日期:星期四打,星期五打),(打球角色:leo、bill、moses、john、Ken)]

到目前为止,我的代码只消除了如图所示的重复项

   NSMutableSet *present = [NSMutableSet set];
    NSUInteger y = 0;

    while (y < [playList count]) {
        id obj = [playList objectAtIndex:y];

        if ([present containsObject:obj]) {

            [playList removeObjectAtIndex:y];

        } else {
            [present addObject:obj];
            y++;
        }
    }
4

1 回答 1

0

您可以创建一个自定义对象,该对象将包含(nssstring playname、nsdate playdate、nsmutablearray playCharacters)等信息。根据您的要求,您可以将此对象添加到 nsarray。然后创建一个函数,该函数将根据名称合并所有对象。在此函数中,它将遍历所有对象,然后将其合并为一个新数组。

-(void)someFunction{
NSMutableArray *newArray=[NSMutableArray array];

for (MyObj *obj in playList) {
    NSMutableArray *present=[playList copy];//perform deep copy rather than shallow
    if ([playList containsObject:obj]) {
        //filter the array by gameName and get the new array containg similar names using nspredicates
        [present filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
            MyObj *customObject=(MyObj *) evaluatedObject;
            return (customObject.gameName!=obj.gameName);
        }]];
        //write code to merge the players * dates
            //LOOP throug the present and merge it to singe object
        //then remove objs from playList array
        [playList removeObjectsInArray:present];
    }
}

}

试试这个我没有测试过的代码,但它可能会工作

于 2013-09-17T06:19:57.340 回答