通过使用filteredArrayUsingPredicate,对数组使用 NSPredicate 相当简单:。
如何对键路径值执行此操作?意思是,我有一个对象数组(在本例中,对象具有相同类型)。每个对象都有一个名为 name 的实例变量。根据文档,它说要执行以下操作:
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"ANY employees.firstName like 'Matthew'"];
这也用在filteredArrayUsingPredicate 中吗?如果我有一系列 People 对象怎么办?这是否意味着我会使用:
NSArray *allPeopleObjects; // pre-populated
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY people.name like 'Foo'"];
NSArray *matching = [allPeopleObjects filteredArrayUsingPredicate:predicate];
该部门的文档有点缺乏。
是否也可以对单个 Person 对象使用 Predicate?像 Person.name 这样的东西包含[cd] 'Foo'?那要怎么做呢?
最佳答案
是的,您的两个示例都可以完美运行。 NSPredicate
可以遵循键值路径。如果您有一个 Person
对象,为什么不直接使用 valueForKeyPath:
并在代码中进行比较,而不是使用 NSPredicate
呢?在这种情况下,这似乎有点矫枉过正。
关于iphone - 如何使用 NSPredicate 作为 Key-Path 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2829952/