DevFly
DevFly

Reputation: 423

Matching array of dictionaries - where one array is not completely equal to the other

There is this hard problem I got to solve, and I'm banging my head for the past couple of days. So I have 2 arrays.

Here is the first one:

 {
        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/";
    },

As you can see 2 objects, dictionaries. In my real app they are much more objects, but this is just an example. The other array:

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

So here is the actual problem. I'm trying to match the second array with the first and combine them, but as you can see the second array only have objects which count is > 0. In my case in the first array my second object's count is 0. So if I match them, after combining to one array, I am left with only one object, while the other one i can't figure out how to put something like count = 0 for it. Here is how I match them:

-(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 is the first array, unreadCount is the second.

I am left with the first object from self.subscriptionsNC, which count is > 0. The other which count is equal to 0 is not added to my combined array, because it's not present in the second array. (which is just overwriting self.subscriptionsNC)

I want if an item from the first array have no count (note: count is not specified nowhere, except from the second array, which doesn't show count equal to 0) to assign a count of 0.

Thank you!

Upvotes: 0

Views: 109

Answers (1)

Carl Veazey
Carl Veazey

Reputation: 18363

While you check to see if you have a match for IDs between the two arrays, you don't ever handle the mismatch scenarios. Although in that case, it gets tricky to do with your loop as you have to constantly check that you haven't already added that item before, and so on. Hope that makes sense, it's late here :)

Here is some code that seemed to solve the problem with data from your example set:

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);

Upvotes: 1

Related Questions