Luke Luke - 15 days ago 5
iOS Question

Why compare the address of a constant with NULL?

#define _UIKeyboardFrameEndUserInfoKey (&UIKeyboardFrameEndUserInfoKey != NULL ? UIKeyboardFrameEndUserInfoKey : @"UIKeyboardBoundsUserInfoKey")


Is there any difference between
UIKeyboardFrameEndUserInfoKey != NULL
and
&UIKeyboardFrameEndUserInfoKey != NULL


I tried:
if(&UIKeyboardFrameEndUserInfoKey != NULL) {...}
and got a warning


Comparison of address of 'UIKeyboardFrameEndUserInfoKey' not equal to
a null pointer is always true

Answer

Comparing the address of a constant (or function) to null is generally used to determine if the constant is defined.

You're getting the warning because the SDK that you're building against (minimum target version) has the constant defined, so it will always evaluate to true (the address won't be null). However, when run on a version of iOS that doesn't have the constant defined, it will not be true (the address will be null).

If you lowered the target version to a version below where the constant were defined, it would weakly link to that SDK, and you would no longer receive the warning.

Read more about Using SDK-Based Development.