user6631314
user6631314

Reputation: 1960

IOS/Objective-C: NSPredicate and NULL values in core data fetch

I am trying to fetch items that do not have a value of 1 in an NSNumber attribute. Some of the items I want to fetch have never had the attribute set at all and therefore have a NSNull value. How can I fetch items that are not 1 but may be NSNull?

The attribute is an NSNumber and I am also a bit confused about whether I should be searching on 1 or @1.

The following code is excluding NSNull values when I want to include them:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"&hasserverid!=1"];

Thanks for any suggestions.

Upvotes: 1

Views: 665

Answers (1)

pfandrade
pfandrade

Reputation: 2419

You can explicitly test for both cases with an OR:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"hasserverid != YES OR hasserverid == NULL"];

By the name of your property, I assume when you say 1 you're actually referring to the value true. Using YES in the predicate format instead of 1 makes your predicate more legible.

Upvotes: 2

Related Questions