1

辞書の配列が 2 つあり、それらを比較したい

実際の辞書構造はFacebookのインタレストリストのようなもので、以下のようになっています

自分と友達の共通の趣味を知りたい

両方のユーザーの関心リストを取得しましたが、created_time が異なるため、関心の辞書を比較している間、共通の辞書を取得していません。

        category = "Musical instrument";
        "created_time" = "2011-06-11T09:10:07+0000";
        id = 113099055370169;
        name = Guitar;

            category = "Musical instrument";
            "created_time" = "2013-09-27T06:02:28+0000";
            id = 113099055370169;
            name = Guitar;

誰でもこれを行う効率的な方法を提案できますか

今私は使用していますが、created_timeが異なるため、共通の利益を与えていません

for (int count = 0; count < [arrFriendsInterest count]; count++)
{
    NSDictionary *dictFriend = [arrFriendsInterest objectAtIndex:count];

    if ([arrMyIntrest containsObject:dictFriend]) {
        [arrMutualInterest addObject:dictFriend];
    }

}

arrFriendsInterest は、友人の関心を含む辞書の配列です。

arrMyIntrest は私の興味を含む辞書の配列です×コメントは 5 分間のみ編集可能です×コメントは 5 分間のみ編集可能です×コメントは 5 分間のみ編集可能です

4

3 に答える 3

0

NSDictionary を使用する代わりに、カスタム クラスを使用しないのはなぜですか?

多くの利点があります。

  • コード補完
  • コンパイル時チェック
  • カスタム isEqual メソッド
  • コードは自明です
于 2013-09-27T11:57:40.300 に答える
0

まず、そのデータを NSArray に保存しますか?

はいの場合は、次のコードを使用する方がはるかに簡単です。

// Do any additional setup after loading the view, typically from a nib.
NSArray *ar1 = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"Musical instrument",@"category",@"2011-06-11T09:10:07+0000",@"created_time",@"113099055370169",@"id", @"Guitar",@"name", nil], nil];


NSArray *ar2 = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"Musical instrument",@"category",@"2013-09-27T06:02:28+0000",@"created_time",@"113099055370169",@"id", @"Guitar",@"name", nil], nil];

NSMutableSet* set1 = [NSMutableSet setWithArray:ar1];
NSMutableSet* set2 = [NSMutableSet setWithArray:ar2];
[set1 unionSet:set2]; //this will give you only the obejcts that are in both sets

NSArray* result = [set1 allObjects];
NSLog(@"%@",[result mutableCopy]);

ハッピーコーディング!!!

于 2013-09-27T11:31:11.693 に答える