0

我必须解决这个难题,过去几天我一直在努力。所以我有2个数组。

这是第一个:

 {
        categories =         (
                        {
                id = "user/06617213952393255174/label/Main Folder";
                label = "Main Folder";
            }
        );
        firstitemmsec = 1297785485208;
        htmlUrl = "http://awebslife.tumblr.com/";
        id = "feed/http://awebslife.tumblr.com/rss";
        sortid = D5DB1FE2;
        title = "awebslife.tumblr.com/rss";
    },
        {
        categories =         (
                        {
                id = "user/06617213952393255174/label/Main Folder";
                label = "Main Folder";
            }
        );
        firstitemmsec = 1344454207448;
        htmlUrl = "http://awholelotofnothing.net";
        id = "feed/http://awholelotofnothing.net/feed/";
        sortid = 7098B8F7;
        title = "awholelotofnothing.net/feed/";
    },

如您所见,有 2 个对象,字典。在我的真实应用程序中,它们是更多的对象,但这只是一个例子。另一个数组:

    {
    count = 4;
    id = "feed/http://awebslife.tumblr.com/rss";
    newestItemTimestampUsec = 1346087733278957;
     },

所以这是实际的问题。我正在尝试将第二个数组与第一个数组匹配并将它们组合,但正如您所见,第二个数组只有计数大于 0 的对象。在我的情况下,在第一个数组中,我的第二个对象的计数为 0。所以如果我匹配它们,在组合到一个数组后,我只剩下一个对象,而另一个我不知道如何为它放置类似 count = 0 的东西。这是我匹配它们的方法:

-(void)gotUnreadCount:(NSDictionary *)unreadCount
{
    NSMutableArray *mutableArr = [NSMutableArray array];

    for (NSDictionary *unreadCountDict in unreadCount) {
        for (NSDictionary *feedDict in self.subcriptionsNC) {
            NSMutableDictionary *feedDictMutable = [feedDict mutableCopy];
            if ([[unreadCountDict objectForKey:@"id"] isEqualToString:[feedDict objectForKey:@"id"]]) {

                [feedDictMutable setObject:[unreadCountDict objectForKey:@"count"] forKey:@"count"];
                [mutableArr addObject:feedDictMutable];
            }
        }
    }
}

self.subscriptionsNC是第一个数组,unreadCount是第二个。

我留下了来自 self.subscriptionsNC 的第一个对象,它的计数 > 0。另一个计数等于 0 的对象不会添加到我的组合数组中,因为它不存在于第二个数组中。(这只是覆盖 self.subscriptionsNC)

如果第一个数组中的一个项目没有计数(注意:除了第二个数组,它不显示计数等于 0 之外,没有指定计数)分配计数为 0。

谢谢!

4

1 回答 1

1

当您检查两个数组之间的 ID 是否匹配时,您永远不会处理不匹配的情况。尽管在这种情况下,使用循环会变得很棘手,因为您必须不断检查您之前是否还没有添加过该项目,依此类推。希望这是有道理的,这里已经很晚了:)

这是一些似乎可以解决示例集中数据问题的代码:

NSDictionary *firstNoCount = @{@"id" : @"feed/http://awebslife.tumblr.com/rss"};
NSDictionary *firstWithCount = @{ @"id" : @"feed/http://awebslife.tumblr.com/rss", @"count" : @4 };
NSDictionary *secondNoCount = @{ @"id" : @"feed/http://awholelotofnothing.net/feed/" };
NSArray *subscriptionsWithoutCount = @[ firstNoCount, secondNoCount ];
NSArray *subscriptionsWithUnreadCount = @[ firstWithCount ];
NSArray *allIds = [subscriptionsWithoutCount valueForKey:@"id"];
NSArray *idsWithUnreadCount = [subscriptionsWithUnreadCount valueForKey:@"id"];

NSIndexSet *indexesWithZeroCount = [allIds indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    return ![idsWithUnreadCount containsObject:obj];
}];

NSMutableArray *combined = [NSMutableArray array];

[subscriptionsWithoutCount enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSMutableDictionary *mutableItemDictionary = [(NSDictionary *)obj mutableCopy];
    NSNumber *unreadCountNumber = nil;
    if ([indexesWithZeroCount containsIndex:idx])
    {
        unreadCountNumber = @0;
    }
    else
    {
        NSInteger indexInCountArray = [idsWithUnreadCount indexOfObject:[mutableItemDictionary objectForKey:@"id"]];
        NSDictionary *countDictionary = [subscriptionsWithUnreadCount objectAtIndex:indexInCountArray];
        unreadCountNumber = [countDictionary objectForKey:@"count"];
    }
    [mutableItemDictionary setObject:unreadCountNumber forKey:@"count"];
    [combined addObject:mutableItemDictionary];
}];

NSLog(@"combined = %@",combined);
于 2012-08-29T09:38:43.007 回答