如何在Core Data中编写BOOL谓词? [英] How to write a BOOL predicate in Core Data?

查看:94
本文介绍了如何在Core Data中编写BOOL谓词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 BOOL 类型的属性,我想搜索所有此属性为 YES

I have an attribute of type BOOL and I want to perform a search for all managed objects where this attribute is YES.

对于字符串属性,它很简单。我创建一个这样的谓词:

For string attributes it is straightforward. I create a predicate like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userName = %@", userName];

但如果我有一个名为的bool属性我想为此做一个谓词?我可以这样做吗?

But how do I do this, if I have a bool attribute called selected and I want to make a predicate for this? Could I just do something like this?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"selected = %@", yesNumber];

或者我需要其他格式说明符,只需传递 YES

Or do I need other format specifiers and just pass YES?

推荐答案

来自谓词编程指南

您指定并测试布尔值的相等性,如以下示例所示:

You specify and test for equality of Boolean values as illustrated in the following examples:

NSPredicate *newPredicate = [NSPredicate predicateWithFormat:@"anAttribute == %@", [NSNumber numberWithBool:aBool]];
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"anAttribute == YES"];

您还可以查看谓词格式字符串语法

这篇关于如何在Core Data中编写BOOL谓词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆