【问题标题】:How to find two NSArray has common NSDictionary如何找到两个 NSArray 有共同的 NSDictionary
【发布时间】:2016-09-23 08:33:25
【问题描述】:

我有一个 NSArray。它有一个或多个 NSDictionary。 otherContacts 在每个索引中都有一个字典。 chatContacts 在每个索引中有两个字典。我怎样才能找到Arraycontact_detail 相同。

NSArray * otherContacts = {
        "contact_detail" =         {
          "contact_Label" = "Test 5 ";
           userid = 48;
        };
   }

NSArray * chatContacts ={
        "contact_detail" =         {
          "contact_Label" = "Test 5 ";
          userid = 48;
        };
        "last_msg_details" =     {
         "Key_from_me" = 1;
         data = " B";
        };
   }

我已经尝试过使用 NSPredicate。但它没有返回公共数据。

NSArray *filtered = [otherContacts filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        return [chatContacts containsObject:evaluatedObject];
    }]];

【问题讨论】:

    标签: ios objective-c iphone search


    【解决方案1】:

    也许您可以使用NSMutableSet 来实现:

    喜欢,

    NSMutableSet* set1 = [NSMutableSet setWithArray:array1];
    NSMutableSet* set2 = [NSMutableSet setWithArray:array2];
    
    //Find intersect: Which will give common objects
    [set1 intersectSet:set2]; 
    
    //Array with common objects
    NSArray* arrCommon = [set1 allObjects];
    
    //Now check common objects count to find if it has common object
    
    if(arrCommon.count>0){
    
      //has common dictionary
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-31
      • 1970-01-01
      相关资源
      最近更新 更多