Search code examples
ioscore-databitmapnspredicate

How to write a Core Data fetch request predicate to compare an NSNumber as a bitmap


I have a Core Data Entity attribute called 'type'. Its of type integer in core data, so a NSNumber when pulled out and the value is a bitmap that I wish to filter on for a specific set of values

I've been trying to compose a NSPredicate for this along the lines of

NSPredicate *typeOfFacilityPredicate = [NSPredicate predicateWithFormat:@"type AND %@ > %0",@6667, @0];

Xcode if giving me *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "type AND %@ > %@"'

Is it possible to perform this logic in a core data fetch predicate predicate ? Or do i have to to make a series of ORs where i compare the type to a number of different values ? or perhaps create an array of values and do something along the lines of ANY type IN

Any ideas on how to correctly write the predicate would be gratefully received.


Solution

  • So having returned to my Mac I was able to check this and, indeed,

    NSPredicate *typeOfFacilityPredicate = [NSPredicate predicateWithFormat:@"bitwiseAnd:with:(type, %@) > %@",@6667, @0];
    

    should work. But having checked the SQL generated by this, I see that a much simpler solution is just to replace "AND" in your predicate with "&":

    NSPredicate *typeOfFacilityPredicate = [NSPredicate predicateWithFormat:@"type & %@ > %@",@6667, @0];