如何在 Core Data 中使用二进制标志?

发布于 2024-10-12 04:24:27 字数 236 浏览 8 评论 0原文

我的 Core Data 数据库中有一个 int32 属性。 我使用这个 int 作为 enum 位字段。

是否可以创建一个 NSPredicate 来基于二进制查询项目 这个 int 的值?像 @"bitFieldAttribute & 0x0001" 这样的东西?

我还想知道二进制类型属性是否可以实现这一点?

I have an int32 attribute in a Core Data database.
I use this int as an enum bit field.

Is it possible to create a NSPredicate to query items based on the binary
value of this int ? Something like @"bitFieldAttribute & 0x0001"?

I'm also wondering if this is possible with a binary typed attribute ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

九歌凝 2024-10-19 04:24:27

NSPredicate 可以处理它,但我不确定 CoreData 是否会接受它作为在数据存储上执行的有效谓词。将按位运算符转换为 SQL 查询时可能会遇到问题(如果您使用的是 SQLite 后备存储)。你只需要尝试一下即可。

然而,语法正是您所期望的:

NSPredicate * p = [NSPredicate predicateWithFormat:@"(3 & 1) > 0"];
NSLog(@"%@", p);
NSLog(@"%d", [p evaluateWithObject:nil]);

日志:

3 & 1 > 0
1

至于在二进制类型的属性上执行此操作(即定义为 data,对吗?),这可能行不通。位运算符只有在对整数进行操作时才真正有意义(据我所知),因此在 NSData 上执行它没有多大意义。先把它转成数字,然后就可以了。

编辑

看来 SQLite 支持这种语法,因为 按位运算符已经存在自 2001 年起,这意味着 Core Data 可能也会接受它。

NSPredicate can handle it, but I'm not sure if CoreData will accept it as a valid predicate for execution on a data store. It might have trouble converting the bitwise operator into a SQL query (if you're using a SQLite backing store). You'll just have to try it.

The syntax, however, is just what you'd expect:

NSPredicate * p = [NSPredicate predicateWithFormat:@"(3 & 1) > 0"];
NSLog(@"%@", p);
NSLog(@"%d", [p evaluateWithObject:nil]);

Logs:

3 & 1 > 0
1

As for doing this on a binary-typed attribute (ie, one defined as data, right?) This probably won't work. Bitwise operators only really make sense when operating on integers (insofar as I understand them), so executing it on an NSData wouldn't make much sense. Convert it to a number first, and then it might work.

edit

It would appear that SQLite supports this syntax, since bitwise operators have been around since 2001, which means that Core Data will probably accept it as well.

二手情话 2024-10-19 04:24:27

rockfakie 几乎是正确的,但却

NSPredicate *someTypePredicate = [NSPredicate predicateWithFormat:@"(typeValue & %i) == %i", valueOneOrThree,valueOneOrThree];

是我所需要的。

rockfakie is so nearly right but

NSPredicate *someTypePredicate = [NSPredicate predicateWithFormat:@"(typeValue & %i) == %i", valueOneOrThree,valueOneOrThree];

Is what I needed.

妳是的陽光 2024-10-19 04:24:27

这是该技术的一个示例/应用。

假设您有一个 NSManagedObject,它具有一个带有键路径“typeValue”的整数属性。

在代码中的某处定义按位枚举:

typedef enum SomeType {
    SomeTypeValueOne = 0x1,
    SomeTypeValueTwo = 0x2,
    SomeTypeValueThree = 0x4
} SomeType;

现在要查询类型为 One 或 Three 但不是 Two 的托管对象,请执行以下操作:

SomeType valueOneOrThree = SomeTypeValueOne | SomeTypeValueThree;

NSPredicate *someTypePredicate = [NSPredicate predicateWithFormat:@"(typeValue & %i) == typeValue", valueOneOrThree];

// construct NSFetchRequest as normal with predicate...

Here is one example / application of this technique.

Say you have a NSManagedObject that has an integer attribute with the keypath "typeValue".

Somewhere in your code define a bitwise enumeration:

typedef enum SomeType {
    SomeTypeValueOne = 0x1,
    SomeTypeValueTwo = 0x2,
    SomeTypeValueThree = 0x4
} SomeType;

Now to query for managed objects that are of type say One or Three but not Two, do the following:

SomeType valueOneOrThree = SomeTypeValueOne | SomeTypeValueThree;

NSPredicate *someTypePredicate = [NSPredicate predicateWithFormat:@"(typeValue & %i) == typeValue", valueOneOrThree];

// construct NSFetchRequest as normal with predicate...
维持三分热 2024-10-19 04:24:27

我几乎不怀疑这一点。

但是您可以对存储在属性中的值使用enum,并使用直接比较而不是位屏蔽。

I hardly doubt it.

But you may use an enum for the values stored in the attribute, and use a direct comparison instead of a bit masking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文