0

I'm trying select values by property with type NSNumber, if property==intValue or property==nil. But this code ignores values with property==nil. What the point?

NSPredicate *predicate = [NSPredicate predicateWithFormat:
        @"(ANY region.beacons.major == %d OR ANY region.beacons.major = nil) 
        AND (ANY region.beacons.minor == %d OR ANY region.beacons.minor = nil)",
        rangedBeacon.major.intValue, rangedBeacon.minor.intValue];

enter image description here

UPD: The most simple doesn't work too:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
                               @"ANY region.beacons.minor = nil"];

UPD 2: Probably problem because of ANY+nil According to this answer Core Data, NSPredicate, ANY key.path == nil ANY select only NOTNULL values.

Community
  • 1
  • 1
Konstantin.Efimenko
  • 1,242
  • 1
  • 14
  • 38
  • Why duplicate? I don't understand the `uint16_t` stuff, since it's not a object, it shouldn't be nil. Why isn't it a `NSNumber`? Aren't you using `CLBeacon` objects? – Larme Sep 12 '16 at 14:28
  • I duplicated this question, because brevios marked as duplicated now and linked on wrong answer. Yes, I'm using CLBeacon. – Konstantin.Efimenko Sep 12 '16 at 14:32
  • Well, according to linked answer, you should write `nil` and not `NIL` anyways in your predicate, modification that your code doesn't reflect. Now, what about the other questions I asked in my previous comment? – Larme Sep 12 '16 at 14:33
  • Larme, sorry, I copipasted previous question in hurry. I corrected NIL into nil and this did not help. – Konstantin.Efimenko Sep 12 '16 at 14:36
  • What was wrong with the duplicate question's answers? The code you've posted here is not how it is shown in the answers of the duplicate of your previous question. – rmaddy Sep 12 '16 at 15:17
  • rmaddy, thous answer doesn't solve my problem – Konstantin.Efimenko Sep 12 '16 at 15:31
  • As stated before: http://stackoverflow.com/questions/895495/how-do-i-test-if-a-primitive-in-objective-c-is-nil I still don't understand why `minor` a `uint16_t`, whereas it should be a `NSNumber`, and why it `major` is a `NSNumber`? – Larme Sep 12 '16 at 15:56
  • It's my fault again. I thought that property value the same type as parameter value in initialization. Yes, it's NSNumber. – Konstantin.Efimenko Sep 12 '16 at 16:04

2 Answers2

0

Simply test for one of the properties of minor; for example:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
                           @"region.beacons.minor.length = 0"];

NSPredicate *predicate = [NSPredicate predicateWithFormat:
                           @"region.beacons.minor.stringValue = nil"];

EDIT I recognized, that you should remove the ANY

If minor is nil, any call would return in nil as well, or 0 for numeric types.

Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107
-1

For one thing, I notice you're using the assignment operator = instead of equality == while comparing with nil.

Also, try comparing with the Singleton NSNull instance [NSNull null].

Vinod Vishwanath
  • 5,821
  • 2
  • 26
  • 40