【问题标题】:Check that the contents of one NSArray are all in another array检查一个 NSArray 的内容是否都在另一个数组中
【发布时间】:2013-02-22 12:57:23
【问题描述】:

我有一个NSArray,其名称在字符串对象中,如下所示:@[@"john", @"smith", @"alex", @"louis"],我还有另一个包含大量名称的数组。如何检查第一个数组中的所有对象是否都在第二个数组中?

【问题讨论】:

标签: objective-c cocoa cocoa-touch collections nsarray


【解决方案1】:

运行一个循环,使用isEqualToStiring来验证mainArray中是否存在array1对象。

【讨论】:

    【解决方案2】:

    您可以使用[NSArray containsObject:] 的概念,您的对象将来自您的array1,就像您说的“john”、“smith”、“alex”、“loui”

    【讨论】:

      【解决方案3】:
      int num_of_matches = 0;
      for(NSString *name in mainArray)
      {
            if(array1 containsObject:name){
            num_of_matches++;
           }
      }
      if(num_of_matches == [array1 count]{
            // All objects present
      }else {
            // Matched number is equal of number_of_matches
      }
      

      【讨论】:

        【解决方案4】:

        如果你只需要检查 array1 中的所有对象是否都在 mainArray 中,你应该使用NSSet 例如

        BOOL isSubset = [[NSSet setWithArray:array1] isSubsetOfSet:[NSSet setWithArray:mainArray]] 
        

        如果你需要检查mainArray中有哪些对象,你应该看看NSMutableSet

        NSMutableSet *array1Set = [NSMutableSet setWithArray:array1];
        [array1Set intersectSet:[NSSet setWithArray:mainArray]];
        //Now array1Set contains only objects which are present in mainArray too
        

        【讨论】:

          【解决方案5】:

          NSSet 具有您正在寻找的功能。

          如果我们暂时忽略性能问题,那么下面的 sn-p 将在一行代码中完成您需要的操作:

          BOOL isSubset = [[NSSet setWithArray: array1] isSubsetOfSet: [NSSet setWithArray: mainArray]];
          

          【讨论】:

          • 我认为这是迄今为止最好的解决方案。我完全忘记了 NSSet。
          • @PraveenS:我同意你的观点 :)
          • 确实是最好的解决方案 :) +1
          【解决方案6】:

          使用 NSArray filteredArrayUsingPredicate: 方法。在两个数组中找出相似类型的对象真的很快

          NSPredicate *intersectPredicate = [NSPredicate predicateWithFormat:@"SELF IN %@", otherArray];
          NSArray *intersectArray = [firstArray filteredArrayUsingPredicate:intersectPredicate];
          

          从上面的代码中,相交数组为您提供了与其他数组中相同的对象。

          【讨论】:

            【解决方案7】:

            试试这个方法;

            NSArray *mainArray=@[@"A",@"B",@"C",@"D"];
            NSArray *myArray=@[@"C",@"x"];
            
            BOOL result=YES;
            for(id object in myArray){
                if (![mainArray containsObject:object]) {
                    result=NO;
                    break;
                }
            }
            NSLog(@"%d",result); //1 means contains, 0 means not contains
            

            【讨论】:

            • 是的,我是从你那里复制过来的:D
            • 哎呀哎呀!你有时让我很生气,我觉得我什至不能再信任或和你说话了! ^^
            • @Filip:别担心你会在这里结交很多其他朋友:p
            【解决方案8】:
             NSArray *array1 = [NSArray arrayWithObjects:@"a", @"u", @"b", @"v", @"c", @"f", nil];
                NSMutableArray *mainArray = [NSMutableArray arrayWithObjects:@"a", @"u", @"I", @"G", @"O", @"W",@"Z",@"C",@"T", nil];
                int j=0;
                for(int i=0; i < mainArray.count; i++)
                {
                    if (j < array1.count)
                    {
                        for( j=0; j <= i; j++)
                        {
                            if([[mainArray objectAtIndex:i] isEqualToString:[array1 objectAtIndex:j]] )
                            {
                                NSLog(@"%@",[mainArray objectAtIndex:i]);
                            }
                        }
                    }
            
                }
            

            【讨论】:

              【解决方案9】:

              使用此代码..

              NSArray *temp1 = [NSArray arrayWithObjects:@"john",@"smith",@"alex",@"loui,@"Jac", nil];
              NSArray *temp2 = [NSArray arrayWithObjects:@"john",@"smith",@"alex",@"loui,@"Rob", nil];
              
              NSMutableSet *telephoneSet = [[NSMutableSet alloc] initWithArray:temp1] ;
              NSMutableSet *telephoneSet2 = [[NSMutableSet alloc] initWithArray:temp2];
              
              
              [telephoneSet intersectSet:telephoneSet2];
              
               NSArray *outPut = [telephoneSet allObjects];
               NSLog(@"%@",outPut);
              

              输出数组包含:

              "john","smith","alex","loui
              

              根据您的要求。

              【讨论】:

              • OP 想要检查其他数组中存在的内容。不创建新数组。这是怎么回答的?
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-10-28
              • 1970-01-01
              • 2021-04-03
              • 2010-09-24
              相关资源
              最近更新 更多