如何在 Core Data 中使用二进制标志?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NSPredicate 可以处理它,但我不确定 CoreData 是否会接受它作为在数据存储上执行的有效谓词。将按位运算符转换为 SQL 查询时可能会遇到问题(如果您使用的是 SQLite 后备存储)。你只需要尝试一下即可。
然而,语法正是您所期望的:
日志:
至于在二进制类型的属性上执行此操作(即定义为
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:
Logs:
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 anNSData
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.
rockfakie 几乎是正确的,但却
是我所需要的。
rockfakie is so nearly right but
Is what I needed.
这是该技术的一个示例/应用。
假设您有一个 NSManagedObject,它具有一个带有键路径“typeValue”的整数属性。
在代码中的某处定义按位枚举:
现在要查询类型为 One 或 Three 但不是 Two 的托管对象,请执行以下操作:
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:
Now to query for managed objects that are of type say One or Three but not Two, do the following:
我几乎不怀疑这一点。
但是您可以对存储在属性中的值使用
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.