nil
is a pointer, but length
is an integer, hence the compiler warning. You could compare length
to 0
, but then that's a legitimate value for a length in an NSRange
. If I needed to have a "not yet initialised" value for currentRange
, I'd choose to set it to:
{ .location = NSNotFound, .length = 0 }
in -init
. That of course supposes that the range could never assume that value in the course of operations. If the range can really take on any of the values in its domain, then you can't use any of them as the placeholder for "not yet initialised".
You could choose to store a pointer to the range which is NULL until the range gets set. Another option would be to use the State pattern to distinguish between the states before and after the range gets set. Another is to design the class and its interface contract such that the range only ever gets used after it's been set.