如何在NSSet或NSArray中搜索具有特定属性的特定值的对象? [英] How to search an NSSet or NSArray for an object which has an specific value for an specific property?
本文介绍了如何在NSSet或NSArray中搜索具有特定属性的特定值的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在NSSet或NSArray中搜索具有特定属性的特定值的对象?
How to search an NSSet or NSArray for an object which has an specific value for an specific property?
示例:我有一个包含20个对象的NSSet,每个对象都有一个type
属性.我想获取第一个具有[theObject.type isEqualToString:@"standard"]
的对象.
Example: I have an NSSet with 20 objects, and every object has an type
property. I want to get the first object which has [theObject.type isEqualToString:@"standard"]
.
我记得有可能以某种方式使用谓词,对吗?
I remember that it was possible to use predicates somehow for this kind of stuff, right?
推荐答案
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type == %@", @"standard"];
NSArray *filteredArray = [myArray filteredArrayUsingPredicate:predicate];
id firstFoundObject = nil;
firstFoundObject = filteredArray.count > 0 ? filteredArray.firstObject : nil;
NB:在NSSet中找到 first 对象的概念没有意义,因为对象在集合中的顺序是不确定的.
NB: The notion of the first found object in an NSSet makes no sense since the order of the objects in a set is undefined.
这篇关于如何在NSSet或NSArray中搜索具有特定属性的特定值的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文