-
此方法返回包含值对象的所有键路径。
-
创建NSArray(Find).h和NSArray(Find).m文件:
NSArray(查找).h:
#import <Foundation/Foundation.h>
@interface NSArray(Find)
- (NSArray *)findAllWhereKeyPath:(NSString *)keyPath equals:(id)value;
@end
@implementation NSArray (Find)
- (NSArray *)findAllWhereKeyPath:(NSString *)keyPath equals:(id)value {
NSMutableArray *matches = [NSMutableArray array];
for (id object in self) {
id objectValue = [object valueForKeyPath:keyPath];
if ([objectValue isEqual:value] || objectValue == value) [matches addObject:object];
}
return matches;
}
两个文件都应该添加到项目中。将NSArray(Find).h导入.m文件,在其中使用您的类别:
#import "NSArray(Find).h"
findAllWhereKeyPath:equals:
那就行了。