Cod*_*kes 21 iphone core-data nspredicate nsfetchrequest bit-fields
我int32在Core Data数据库中有一个属性.我用它int作为一个enum位字段.
是否可以NSPredicate根据此int的二进制值创建查询项?有点像@"bitFieldAttribute & 0x0001"?
我也想知道这是否可以使用二进制类型属性?
Dav*_*ong 26
NSPredicate可以处理它,但我不确定CoreData是否会接受它作为在数据存储上执行的有效谓词.它可能无法将按位运算符转换为SQL查询(如果您使用的是SQLite后备存储).你只需要尝试一下.
然而,语法正是您所期望的:
NSPredicate * p = [NSPredicate predicateWithFormat:@"(3 & 1) > 0"];
NSLog(@"%@", p);
NSLog(@"%d", [p evaluateWithObject:nil]);
Run Code Online (Sandbox Code Playgroud)
日志:
3 & 1 > 0
1
Run Code Online (Sandbox Code Playgroud)
至于在二元类型属性上执行此操作(即,定义为data,对吗?)这可能不起作用.在按整数运算时(在我理解它们的情况下),按位运算符才真正有意义,所以在a上执行它就NSData没有多大意义.首先将其转换为数字,然后它可能会起作用.
编辑
似乎SQLite支持这种语法,因为逐位运算符自2001年以来就存在,这意味着Core Data也可能会接受它.
rockfakie是如此接近正确但是
NSPredicate *someTypePredicate = [NSPredicate predicateWithFormat:@"(typeValue & %i) == %i", valueOneOrThree,valueOneOrThree];
Run Code Online (Sandbox Code Playgroud)
是我需要的.
这是该技术的一个示例/应用.
假设您有一个NSManagedObject,它具有带键路径"typeValue"的整数属性.
代码中的某处定义了按位枚举:
typedef enum SomeType {
SomeTypeValueOne = 0x1,
SomeTypeValueTwo = 0x2,
SomeTypeValueThree = 0x4
} SomeType;
Run Code Online (Sandbox Code Playgroud)
现在,要查询类型为一个或三个但不是两个的托管对象,请执行以下操作:
SomeType valueOneOrThree = SomeTypeValueOne | SomeTypeValueThree;
NSPredicate *someTypePredicate = [NSPredicate predicateWithFormat:@"(typeValue & %i) == typeValue", valueOneOrThree];
// construct NSFetchRequest as normal with predicate...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4314 次 |
| 最近记录: |