ios - 谓词为零,nscoredata,目标

标签 ios objective-c core-data nspredicate predicatewithformat

我正在做的是获取核心数据的数据如下

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/

相关文章:

ios - 如果不在 ViewController 中,则估计不会调用 didChangeAuthorizationStatus

ios - 如何将数据从 Core Data 迁移到 Ruby On Rails?

ios - 另一个实体的核心数据过滤器

ios - CSStickyHeaderFlowLayout swift 接收器没有带标识符的 segue

ios - Codename One 中使用 native code 时参数可以随意命名吗?

ios - 对具有字符串文字和 NSObject 下标类型的下标的引用不明确

objective-c - OpenGL GL_POINTS 大小

objective-c - 使用 Core Data 时提高 UI 性能

ios - 使用 Storyboard xcode6 在 View Controller 之间传递数据

ios - 如何在不完全下载PDF文件的情况下生成缩略图?