ios - 如何查找数组是否包含两个或多个对象

标签 ios nsarray

我正在尝试实现 containsObject,但有两个或更多参数,这可能吗?

目前我有:

而且显然争论太多了。我已经研究了苹果的文档,但还没有找到任何东西。有什么建议吗?

if ([ myArray containsObject:@"1", @"2"]){
    NSLog(@"if");
} else if([ myArray containsObject:@"1", @"2",@"3",@"4"]) {
   NSLog(@"else if");
}else if([ myArray containsObject:@"1", @"2",@"3"]) {
   NSLog(@"else");
}

我的数组:-

myArray is (
    1,
    2,
    3,
    4
)

最佳答案

你可以检查数组的子集

 NSArray *arry1= [NSArray arrayWithObjects:@"1",@"2",@"3",@"4", nil];
        NSArray *arry2= [NSArray arrayWithObjects:@"1",@"2", nil];

        NSSet *set1 = [NSSet setWithArray:arry1];
        NSSet *set2 = [NSSet setWithArray:arry2];

        if ([set2 isSubsetOfSet:set1])
        {
            NSLog(@"array1 contains all elements of array 2");
        }else{
            NSLog(@"array1 does not contains all elements of array 2");
        }

关于ios - 如何查找数组是否包含两个或多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36261267/

相关文章:

ios - 第一次刷新表格时,圆形图像是方形的

ios - 更改空 TableView 单元格的颜色?

objective-c - 如何从格式字符串(如 @"xxx=%@, yyy=%@")和对象的 NSArray 创建 NSString?

iphone - 测试 NSMutableArray 是否包含字符串对象

ios - webview 不显示在 iphone 6 的全屏上

ios - 为什么数组中的最后一个对象出现了?

iphone - 如何捕获自定义uitableViewCell中的开关更改事件?

ios - 如何从一个数组中获得具有其他属性的对象?

ios - 如何在表格 View 中隐藏行。隐藏索引后应该不会改变 iOS

objective-c - 为什么我的数组中的每个其他对象都是空白的?