如何在Core Data中使用二进制标志? [英] How to use binary flags in Core Data?

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

问题描述

我在Core Data数据库中有一个 int32 属性。
我使用 int 作为枚举位字段。



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



我也想知道如果这是可能的二进制类型化属性?

解决方案

NSPredicate 可以处理,不确定CoreData是否会将其作为在数据存储上执行的有效谓词接受。它可能无法将位运算符转换为SQL查询(如果您使用SQLite后备存储)。



但是,语法只是你想要的:

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

日志:

 code> 3& 1。 0 
1

对于二进制类型的属性作为 data ,对吗?)这可能不工作。当对整数操作时(只要我理解它们),位运算符才真正有意义,因此在 NSData 上执行它并不有意义。



>看起来SQLite支持这种语法,因为按位操作符自2001年以来一直存在,这意味着Core Data也可能接受它。


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 ?

解决方案

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.

这篇关于如何在Core Data中使用二进制标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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