0

In the NSFetchedResultsController for a tableview, I am trying to filter out cases where an attribute is true, 1, yes, on or whatever.

I am unclear on whether the NSPredicate is not acting as it should because of 1) how my syntax handles nil or null values or 2) because it is not correct for the type of attribute.

In the xcdatamodeld file, the attribute is set to Boolean.

In the NSManagedObject class, I have used an NSNumber property--because to my knowledge, there is no Boolean property and one is supposed to substitute NSNumber.

@property (nonatomic, strong) NSNumber *isprivate;//boolean in model.

In some cases I have set the value to @1. However, in other cases it should be set to nil, though when I log it out it shows as null. I want to exclude from a fetch cases where the value of isprivate is true, yes, @1 or whatever.

My NSPredicate code is:

 NSNumber *privVal = @1;          
 NSPredicate *securePred = [NSPredicate predicateWithFormat:@"isprivate!= %@",privVal];

which logs to console as isprivate != 1

However, it is not working as desired. Rather than filter out cases where isprivate is 1, it filters everything and returns no results.

So maybe it is interpreting nil or null values wrong.

The second possibility is that the syntax is wrong for Boolean/NSNumbers or whatever.

Would appreciate any suggestions on how to filter out nsmanagedobjects where a Boolean value set as above is True.

Thanks in advance for any suggestions.

user6631314
  • 1,751
  • 1
  • 13
  • 44

2 Answers2

0

Did you try the code below, I usually use Integer for bool values. Don’t forget to change the type in CoreData

NSInteger privVal = 1;          
NSPredicate *securePred = [NSPredicate predicateWithFormat:@"isprivate != %d",privVal];
matt
  • 515,959
  • 87
  • 875
  • 1,141
Ionescu Vlad
  • 151
  • 6
0

When using predicates with Boolean values you can use @NO or @YES.

NSPredicate *securePred = [NSPredicate predicateWithFormat:@"isprivate == %@", @YES]; I answered a similar question here https://stackoverflow.com/a/29380640/4698501

Could the problem be your property should be isPrivate instead of isprivate?

Patrick
  • 2,035
  • 17
  • 27