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;
     },

ここに実際の問題があります。2 番目の配列を最初の配列と一致させて結合しようとしていますが、ご覧のとおり、2 番目の配列にはカウント > 0 のオブジェクトしかありません。私の場合、最初の配列では、2 番目のオブジェクトのカウントは 0 です。それらを一致させて、1 つの配列に結合した後、1 つのオブジェクトだけが残りますが、もう 1 つのオブジェクトには 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は 2 番目の配列です。

カウントが > 0 である self.subscriptionsNC からの最初のオブジェクトが残っています。カウントが 0 に等しいもう 1 つのオブジェクトは、2 番目の配列に存在しないため、結合された配列に追加されません。(これは self.subscriptionsNC を上書きするだけです)

最初の配列の項目にカウントがない場合 (注: 0 に等しいカウントを表示しない 2 番目の配列を除いて、カウントはどこにも指定されていません)、カウント 0 を割り当てます。

ありがとうございました!

4

1 に答える 1

1

2 つのアレイ間で 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 に答える