如何使用NSPredicate过滤数组中的对象 - > _title变量等于变量title?我尝试了以下内容,但它没有过滤任何内容。
NSMutableArray *array = [[posts mutableCopy] autorelease];
NSString *title = [[TBForrstr sharedForrstr] stringForPostType:type];
[array filterUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"SELF->_title == %@", title]]];
答案 0 :(得分:1)
确保标题与数组完全匹配。或者,如果您想要更灵活,请将其设置为不区分大小写:
NSMutableArray *array = [[posts mutableCopy] autorelease];
NSString *title = [[TBForrstr sharedForrstr] stringForPostType:type];
[array filterUsingPredicate:[NSPredicate predicateWithFormat:@"_title == [cd]%@", title]];
答案 1 :(得分:1)
这个对我有用 - 请注意字符串周围的单引号:
NSArray *posts = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys: @"Cat or dog?", @"_title", nil],
[NSDictionary dictionaryWithObjectsAndKeys: @"I saved a file, where is it?", @"_title", nil],
[NSDictionary dictionaryWithObjectsAndKeys: @"How should I do this?", @"_title", nil],
nil];
NSMutableArray *array = [[posts mutableCopy] autorelease];
NSString *title = @"Cat or dog?";
[array filterUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"SELF._title == '%@'", title]]];
NSLog(@"%@", array);
答案 2 :(得分:0)
只需使用:
[NSPRedicate predicateWithFormat:@"title LIKE %@",title]
标题应通过KVC
与您的_title ivar相匹配