【发布时间】:2015-04-15 09:52:05
【问题描述】:
我正在使用核心数据来获取对象列表。这些对象有一个名为“类别”的属性。这个 category 属性是一个 NSDictionary,由 json 构造而成:
@{@"A":@YES, @"B":@NO, @"C":@YES}
我想获取所有核心数据对象,哪个类别对于给定的类别键是正确的。 我试过了:
NSString *categoryKey = @"A";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"categories.%K == YES", categoryKey]];
// or
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.categories.%@ == YES", categoryKey]];
// or
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"categories[%@] == YES", categoryKey];
// Throws exception : Unsupported function expression categories["A"]
// or
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"categories[%K] == YES", categoryKey];
// Throws exception : Unsupported function expression categories[A]
// or
NSString *categoryKeyPath = [NSString stringWithFormat:@"categories.%@", categoryKey];
NSPredicate *p = [NSPredicate predicateWithFormat:@"%K == YES", categoryKeyPath]];
但是在执行我的 fetch 时它总是返回空数组(当然我有一些带有categories[@"A"] = YES 的对象)。
我认为问题出在嵌套的字典键路径上,但我找不到用一个谓词来实现这一点的方法。
编辑:
为了澄清我会使用
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
return [[evaluatedObject.categories objectForKey:categoryKey] boolValue];
}];
但是predicateWithBlock:在核心数据中获取时不支持。
【问题讨论】:
标签: ios objective-c core-data nsdictionary nspredicate