What is the availability of NSNotFound?

This Xcode documentation for NSNotFound rather confusing:

It says: "Available in iOS 2.0 to 8.4" and "Availability: iOS 8.1 to 8.0." So ... Is it available before 8.0? Or in 9.0+? Also, what happens here, if so?

+7
ios foundation code-documentation
source share
2 answers

Insert availabilityOfNSNotFound == NSNotFound jokes here.


At some point, when Apple insisted on mandatory support for 64-bit devices (iOS 8.4 SDK?), The NSNotFound announcement was changed from:

 enum {NSNotFound = NSIntegerMax}; 

to

 static const NSInteger NSNotFound = NSIntegerMax; 

You can check this in <Foundation/NSObjCRuntime.h> .

The documentation has never changed, so the availability of enum NSNotFound no longer in the SDK. But with iOS 9 and above, static const NSInteger NSNotFound .

Although I can’t answer the true availability of NSNotFound since I don’t work for Apple (as a developer, I think it is safe to use in all versions of iOS since version 2.0, otherwise many Foundation classes will break, since they can return NSNotFound ) , you can check if the memory location for NSNotFound NULL:

 #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wtautological-compare" BOOL found = (&NSNotFound != NULL); #pragma clang diagnostic pop if (found) { NSLog(@"meh"); } 
+7
source share

I realized that NSNotFound is a static constant, so it should not matter which OS the device has, since the value should not change after compilation.

To confirm this, I made the simplest file that I could and looked at its compiled assembly (without optimization):

Left: source code. Right: LLVM build exit.

Illustration showing that NSNotFound is replaced with its absolute value at compile time

As you can see, NSNotFound is replaced with its absolute value, in this case 0x7fffffffffffffff , since it is a 64-bit compilation. For 32-bit compilations, this will be 0x7fffffff .

This is amazing. This means that while it compiles, it will work (if Apple never changes the value of NSNotFound )!

Although this does not explain the weird documentation, it does provide some confirmation that it should work on all versions of iOS.

+1
source share

All Articles