4

The answers to this question state that if (x != nil) is the same as if (x).

But Apple documentation reads:

Note: When checking for the existence of a symbol, you must explicitly compare it to NULL or nil in your code. You cannot use the negation operator ( ! ) to negate the address of the symbol.

Which seems to contradict "Working with nil" from https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html#//apple_ref/doc/uid/TP40011210-CH4-SW22

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

3

The documentation is stating that you can't negate the address. In other words, you can't do this:

if (!MyWeakLinkedFunction) {
    // symbol doesn't exist
}

You must instead do:

if (MyWeakLinkedFunction == NULL) {
    // symbol doesn't exist
}

However, both of these work:

if (MyWeakLinkedFunction) {
    // symbol exists
}

or:

if (MyWeakLinkedFunction != NULL) {
    // symbol exists
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • So, despite `NULL` being (most likely) _defined_ as `(void*) 0`, applying the negation operator to that does not give something that evaluates to `true`? – Nicolas Miari Jan 06 '16 at 06:02
  • @NicolasMiari I don't understand your question. The negation operator isn't be applied to `NULL` anywhere in my answer. – rmaddy Jan 06 '16 at 06:05
  • 1
    I just assumed that if `MyWeakLinkedFunction == NULL` evaluates to `true`, then `MyWeakLinkedFunction` should be actually `NULL` (a.k.a. `0`), and negating that would give `true`. I mean, it still isn't clear **why** `! MyWeakLinkedFunction` isn't meaningful/allowed. – Nicolas Miari Jan 06 '16 at 06:10
  • 1
    @NicolasMiari I'm not 100% sure but I think it has to do with the fact that it is a runtime check for the existing of a weakly linked function so the negation doesn't quite work as one might expect with a "normal" pointer. – rmaddy Jan 06 '16 at 06:13
  • I see. I don't know all the implementation details of weak linking at compile time/run time, but it would be an interesting read. I'll do some research... – Nicolas Miari Jan 06 '16 at 06:16
  • @rmaddy, I do not get why `if (MyWeakLinkedFunction)` would be working as expected, but not `if (!MyWeakLinkedFunction)`. – Cœur Jan 06 '16 at 06:24
  • As I said in my reply to Nicolas, I'm not really sure why. It's Apple's statement. I'm simply clarifying their statement in my answer to your question. I do know that `if (MyWeakLinkedFunction)` works because I use that in several places in my own app. I don't I've actually tried the negation because I'm always checking if the symbol exists. – rmaddy Jan 06 '16 at 06:27
  • If the expression is treated as a BOOL, the symbol could exist and `! symbol` could evaluate to `TRUE`. I believe Mike Ash has a post about it, but I can't find it now. – Avi Jan 06 '16 at 08:38
1

IMO, we might just be overthinking.

When checking for the existence of a symbol, you must explicitly compare it to NULL or nil in your code.

This part only says - If you want to check for the existence(and not the non-existence), you must explicitly compare to NULL or NIL

You cannot use the negation operator ( ! ) to negate the address of the symbol.

If you succeed negating the address of the symbol, its only that you land up successfully checking the non-existence(and not the existence).

When i came across the following, I thought this might be a possibility as they use the word availability here -

Note: To check the availability of a function, explicitly compare its address to NULL or nil. You cannot use the negation operator ( ! ) to negate the address of a function to check its availability. Also, note that the name of a C function is synonymous with its address. That is, &myFunction is equivalent to myFunction.

Rajeev Ranjan
  • 3,588
  • 6
  • 28
  • 52