8

Just trying to figure out what's the best practice: when using method that takes (NSError**), is it better to send it nil or NULL?

For example,

NSArray *items = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];

In the documentation it says "You may specify nil for this parameter if you do not want the error information." On the other hand, since its a double pointer, NULL seems to make sense as well?

Joseph Lin
  • 3,324
  • 1
  • 29
  • 39

1 Answers1

12

Technically, NULL is the right answer and the docs are wrong.

In practice, it matters not. NULL and nil are the same, for all intents and purposes.

While that could change and remain language compliant, it couldn't change without breaking tons and tons of stuff.

Feel free to file a bug, though.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • 4
    They differ in their types. They're all zero, but "NULL" is a void *, "nil" is an id, and "Nil" is a Class pointer. http://stackoverflow.com/questions/1564410/when-to-use-nil-and-null-in-objective-c – ARC Oct 13 '11 at 02:48
  • Yup; thanks for the link. And this is why, in this case, it should be NULL, because nil/Nil neither represent pointers to pointers to types; it'd end up being something like (void*id*) or some such cast nonsense. – bbum Oct 13 '11 at 05:01
  • Thanks guys. What I'm worried is that if `NULL` somehow got dereferenced in the method, wouldn't it cause problems? According to this post [http://stackoverflow.com/questions/3581544/exception-codes-kern-protection-failure-at-0x00000000-error], `nil` seems to be a safer choice? – Joseph Lin Oct 14 '11 at 20:07
  • `nil` or `NULL` matters not; if you de-reference either, you'll crash. Since it is a `(NSError**)`, it'd actually be something like `(NSError*void*)` and, thus, `NULL` is really the right choice. But, again, it does't much matter in practice. – bbum Oct 14 '11 at 21:25