我正在做的是获取核心数据的数据如下
NSString *str;
NSPredicate *predicate;
switch (typeCube) {
case NewCubes: {
str = [NSString stringWithFormat:@"type == %d",NewCubes];
break;
}
case AllCubes: {
str = [NSString stringWithFormat:@"type != %d",CustomCubes];
break;
}
case CustomCubes: {
str = [NSString stringWithFormat:@"type == %d",CustomCubes];
break;
}
}
predicate = [NSPredicate predicateWithFormat:@"%@ AND (accounts.keyChainId == %@)", str, accountKeyChainId];
但是,predicate 的结果是 nil。但是下面的作品
predicate = [NSPredicate predicateWithFormat:@"(type == %d) AND (accounts.keyChainId == %@)", type, accountKeyChainId]; ( type is either NewCubes or AllCubes or CustomCubes)
如果您有任何想法,请提供帮助。欢迎所有评论。谢谢
最佳答案
有一个名为 NSCompoundPredicate
的类,它允许您使用 AND、OR 和 NOT 运算符构造谓词。这里需要
[NSCompoundPredicate andPredicateWithSubpredicates:@[predicate1, predicate2, etc.]];
所以代码将是——
NSPredicate *predicate1;
NSPredicate *predicate;
switch (typeCube) {
case NewCubes: {
predicate1 = [NSPredicate predicateWithFormat:@"type == %d",NewCubes];
break;
}
case AllCubes: {
predicate1 = [NSPredicate predicateWithFormat:@"type != %d",CustomCubes];
break;
}
case CustomCubes: {
predicate1 = [NSPredicate predicateWithFormat:@"type == %d",CustomCubes];
break;
}
}
predicate = [NSPredicate predicateWithFormat:@"accounts.keyChainId == %@", accountKeyChainId];
[NSCompoundPredicate andPredicateWithSubpredicates:@[predicate1, predicate]];
在这里阅读更多关于 NSCompountPredicates 的信息:http://nshipster.com/nspredicate/
关于ios - 谓词为零,nscoredata,目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22563956/