在NSMutable Array中查找多个值

时间:2021-10-11 21:46:40

I want to find multiple values in one NSMutableArray. Like if this two values found then in this array then i want to execute my condition.For Example this is my array

我想在一个NSMutableArray中找到多个值。就像在这个数组中找到这两个值然后我想执行我的条件。例如这是我的数组

NSMutableArray findValues have 1,2,3,4,5.Now i want to put condition like this

NSMutableArray findValues有1,2,3,4,5。现在我想放这样的条件

if([findvalues have string @"1"] && [findValues have string @"4"]){

    //execute code

}

can anyone suggest how to do this?

任何人都可以建议如何做到这一点?

4 个解决方案

#1


3  

You can use following code to find object in Array,

您可以使用以下代码在Array中查找对象,

if ([array containsObject:@"1"] && [array containsObject:@"4"]) {
        //execute code
    }

#2


2  

use containsObject:

if([findvalues containsObject:@"1"] && [findValues containsObject:@"4"]){

    //execute code
}

#3


2  

Use below code:

使用以下代码:

  if ([findvalues containsObject:@"1"] && [findvalues containsObject:@"4"]) {

    // Do something here...
  }

#4


0  

To filter array by multiple values, use NSPredicate

要按多个值过滤数组,请使用NSPredicate

Create an object class, which contains an object value and compare the text by using given format

创建一个对象类,其中包含一个对象值,并使用给定的格式比较文本

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"(value   contains[c] %@) || (value contains[c] %@)", @"f", @"x"];

NSMutableArray* list = [NSMutableArray arrayWithArray:[findValues filteredArrayUsingPredicate:predicate]];

Hope this solution helps you.. Thanks

希望这个解决方案能帮到你..谢谢

#1


3  

You can use following code to find object in Array,

您可以使用以下代码在Array中查找对象,

if ([array containsObject:@"1"] && [array containsObject:@"4"]) {
        //execute code
    }

#2


2  

use containsObject:

if([findvalues containsObject:@"1"] && [findValues containsObject:@"4"]){

    //execute code
}

#3


2  

Use below code:

使用以下代码:

  if ([findvalues containsObject:@"1"] && [findvalues containsObject:@"4"]) {

    // Do something here...
  }

#4


0  

To filter array by multiple values, use NSPredicate

要按多个值过滤数组,请使用NSPredicate

Create an object class, which contains an object value and compare the text by using given format

创建一个对象类,其中包含一个对象值,并使用给定的格式比较文本

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"(value   contains[c] %@) || (value contains[c] %@)", @"f", @"x"];

NSMutableArray* list = [NSMutableArray arrayWithArray:[findValues filteredArrayUsingPredicate:predicate]];

Hope this solution helps you.. Thanks

希望这个解决方案能帮到你..谢谢