ioshealthkitnsuinteger

Determine if NSUInteger has a value or is nil


I'm trying to use a HKAnchoredObjectQuery on iOS.

To do so I have to provide an anchor to the query which in this case is a NSUInteger. I want to save this integer for queries in the future in the NSUserDefaults and just simply want to use

if(anchor != nil) { // do stuff }

But the complier tells me that I'm not allowed to do this because I'm comparing a integer to a pointer and I understand why I'm not able to do this but I don't know how to do it.

The error code is this

Comparison between pointer and integer ('NSUInteger' (aka 'unsigned long') and 'void *')

Does anyone know how I'm able to determine if my anchor has a value or not?


Solution

  • NSUInteger is a primitive type, not an object type. NSNumber is an object type. For an NSNumber object, you can check whether it is nil or not. An NSUInteger is never nil because it is an integer, not a pointer.

    Many functions that return NSUInteger as a result for a search return the constant NSNotFound if the search failed. NSNotFound is defined as a very large integer (2^31 - 1 for 32 bit, 2^63 - 1) for 64 bit).