我有一个对象数组。每个对象都有自己的属性(名称,desc,状态)。 name
和desc
是NSString
和status
是BOOL
。使用自定义对象属性过滤器数组
我想通过status属性过滤这个数组。例如:使用status == YES
获取所有对象。
我该如何做到这一点?
我有一个对象数组。每个对象都有自己的属性(名称,desc,状态)。 name
和desc
是NSString
和status
是BOOL
。使用自定义对象属性过滤器数组
我想通过status属性过滤这个数组。例如:使用status == YES
获取所有对象。
我该如何做到这一点?
尝试使用NSPredictate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"status = %@", @"YES"];
NSArray *filterArray = [array filteredArrayUsingPredicate:predicate];
这给你所有的对象,其中的地位是平等是一个数组。
谢谢,它的工作原理和答案是第一次。 – erekle
试试这个,
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"status = YES"];
NSArray *filteredArray = [yourArray filteredArrayUsingPredicate:predicate];
为什么要复制其他答案? – Ilario
这不是复制的答案。 – Akhilrajtr
您可以发布正确的答案,但请检查是否有人像您一样发帖。然后张贴您的答案或给一些更多的描述... – Mani
你好你可以试试这个,
NSMutableArray *array =[NSMutableArray arrayWithObjects:@"Apple", @"Animal", @"baby", @"ball", nil];
NSPredicate *aPredicate =
[NSPredicate predicateWithFormat:@"SELF beginswith[a] 'b'"];
NSArray *beginWithA =
[array filteredArrayUsingPredicate:aPredicate];
The beginWithA array will have { @"Apple", @"Animal" }.
NSPredicate *bPredicate =
[NSPredicate predicateWithFormat:@"SELF contains[b] 's'"];
[array filterUsingPredicate:bPredicate];
The array will have { @"baby", @"ball" }
感谢
这并不回答这个问题。 – vikingosegundo
它帮助,如果你提供的,你已经尝试哪些细节。 – Amar
@Kumar,感谢您的编辑。我会预见下一次。 – erekle