1

When calling a method that has a completion handler as one of it's arguments, if I don't plan on using it, what is an appropriate argument to use? e.g.

 [self.navigationController dismissViewControllerAnimated:YES completion:NULL];

There are lots of examples of the differences between NULL and nil, I was thinking NULL as I think completion handlers are more of a C++ thing and not specific to Objective-c, but I may be barking up the wrong tree.

Do I use NULL, nil or something else?

EDIT: Sorry I should have been more clear, I understand that nil and NULL are equivalent and it's convention to use nil for objects and NULL for pointers. I was asking more about which one I use for a completion handler.

mdb29
  • 53
  • 7
  • 1
    They are equivalent, but by convention, `nil` is used for objects, `Nil` is used for class objects, and `NULL` is generally used for all other pointer types (including out pointers to objects, like `NSError **`. – Itai Ferber Feb 14 '18 at 00:11
  • Apple's own documentation guides shows `nil` for block pointers such as completion handlers. – rmaddy Feb 14 '18 at 00:29

2 Answers2

0

Apple uses NULL in at least some of their Objective-C docs. Here's an example.

For a general discussion of nil vs NULL in Objective-C, see this SO.

I've just used NULL in Obj-C to match Apple's docs. In Swift, use nil.

Smartcat
  • 2,834
  • 1
  • 13
  • 25
0

Xcode has a "Jump to Definition" feature that can be used to answer this question.

Starting with nil takes us to MacTypes.h

#define nil __DARWIN_NULL

Following __DARWIN_NULL to _types.h we have

#define __DARWIN_NULL ((void *)0)

And starting from NULL takes us to stddef.h

#define NULL ((void*)0)

So nil and NULL are the same thing. By convention, nil is used for objects, and NULL is used for other pointer types.

The completion handler takes a block as an argument, and a block is an Objective-C object so by convention, nil should be used when the completion block is not needed.

user3386109
  • 34,287
  • 7
  • 49
  • 68
  • So I guess my next question would be in this case, is a completion handler (a block) a pointer? I realise it is just convention and won't effect what happens. – mdb29 Feb 14 '18 at 00:19
  • @mdb29 I fixed up the answer to better match the updated question. – user3386109 Feb 14 '18 at 00:34