Iphone 如何对关键路径值使用NSPredicate

Iphone 如何对关键路径值使用NSPredicate,iphone,cocoa,nspredicate,objective-c,Iphone,Cocoa,Nspredicate,Objective C,使用filteredArrayUsingPredicate:为数组使用NSPredicate相当直接 如何对关键路径值执行此操作?也就是说,我有一个对象数组(在本例中,对象是相同类型的)。每个对象都有一个名为name的实例变量。根据文件规定,应执行以下操作: NSPredicate *predicate = [NSPredicate predicateWithFormat: @"ANY employees.firstName like 'Matthew'"]; 在filteredArra

使用filteredArrayUsingPredicate:为数组使用NSPredicate相当直接

如何对关键路径值执行此操作?也就是说,我有一个对象数组(在本例中,对象是相同类型的)。每个对象都有一个名为name的实例变量。根据文件规定,应执行以下操作:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
   @"ANY employees.firstName like 'Matthew'"];
在filteredArrayUsingPredicate中是否也使用了该选项?如果我有一个人物对象数组呢?这是否意味着我会使用:

NSArray *allPeopleObjects; // pre-populated
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY people.name like 'Foo'"];
NSArray *matching = [allPeopleObjects filteredArrayUsingPredicate:predicate];
那个部门的文件有点缺乏


也可以对单个Person对象使用谓词吗?诸如Person.name之类的内容包含[cd]“Foo”?如何做到这一点?

是的,您的两个示例都将非常有效
NSPredicate
可以遵循键值路径。如果您有一个
Person
对象,为什么不使用
valueForKeyPath:
并在代码中进行比较,而不是使用
NSPredicate
?在这种情况下,这似乎太过分了。

Alex,我不太确定如何对单个Person对象传递valueForKeyPath。我的意思是,我可以使用NSRange,但我想知道我是否可以像[person valueForKeyPath:@“name like foo”]这样键入代码。
[[person valueForKeyPath:@“name”]rangeOfString:@“Matthew”]位置!=NSNotFound