Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSNotFound, NSInteger, NSUInteger, and NSRange.location

Tags:

compare

cocoa

NSNotFound is defined as NSIntegerMax, which is defined as LONG_MAX.

NSRange.location is defined as a NSUInteger.

Some Foundation methods return an NSRange whose location value is NSNotFound.

So, one often does this comparison:

if (aRange.location == NSNotFound) 

But isn't that a comparison between signed and unsigned integers that should generate a warning?

like image 732
Ross Carter Avatar asked Feb 03 '23 17:02

Ross Carter


1 Answers

You are comparing an unsigned (64 bit) variable with a signed constant which also fits into the range of the variable (it is not negative). So no need for a warning.

The warnings appear typically when comparing against negative constants or variables on both sides (i.e. one part is or can be negative while the other is only positive or zero).

A side note: NSNotFound is different on 32 bit vs 64 bit platforms, so don't hardcode or store that value anywhere.

like image 139
Eiko Avatar answered May 19 '23 02:05

Eiko