Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why NSNotFound isn't -1 but NSIntegerMax

Tags:

objective-c

NSNotFound is defined as NSIntegerMax, which has different values on 32-bit and 64-bit, this brings a lot of inconvenience for persistent and distributed env. Why not define it as -1?

PS, In ObjC and Cocoa, some indexOf... methods returns NSNotFound but some returns -1, is there a reason these results are inconsistent.

like image 330
kliu Avatar asked Feb 18 '12 03:02

kliu


2 Answers

This note from the docs is probably relevant:

Special Considerations

Prior to Mac OS X v10.5, NSNotFound was defined as 0x7fffffff. For 32-bit systems, this was effectively the same as NSIntegerMax. To support 64-bit environments, NSNotFound is now formally defined as NSIntegerMax. This means, however, that the value is different in 32-bit and 64-bit environments.

like image 105
jscs Avatar answered Dec 26 '22 02:12

jscs


Well the answer probably isn't because it is sometimes returned as an unsigned value - in Objective-C conversions between signed and unsigned are effectively bit-copies so you can compare an unsigned to -1 and get the answer you expect (the values is all ones - -1 as signed, maximum integer as unsigned). See answer to this question.

So we come to your second part of the question, why in the inconsistency? Well variety is the spice of life, or put another way there's nowt so queer as folk - people are just inconsistent, no more complicated than that!

like image 20
CRD Avatar answered Dec 26 '22 02:12

CRD