2

What is the difference between return nil declared in a method and void for a method? How can I use them?

RJ27
  • 23
  • 6
  • possible duplicate of [What's the difference between a null pointer and a void pointer?](http://stackoverflow.com/questions/3581585/whats-the-difference-between-a-null-pointer-and-a-void-pointer) – prem30488 Aug 17 '14 at 05:39

3 Answers3

3

Use void when there's never anything to return. Use a non-void data type when the method generally/sometimes would return something. And when this method that would generally/sometimes return some object wants to not return anything (perhaps because some error occurred in the creation of the object), it would return nil.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
2

void is not a return value. It means that method should not return something at all, it just does some things. In programming theory such method is called "procedure". When method is intended to return some object, it can return nil in cases when object not found for example. And checking for this nil you will know that object not found.

m8labs
  • 3,671
  • 2
  • 30
  • 32
0

Method return type should be void incase you want that method to return nothing. An object returning method returns nil incase of unexpected or erroneous conditions. Since passing any message to nil does nothing in Objective C, so your code does not likely to crash and becomes robust.

dev gr
  • 2,409
  • 1
  • 21
  • 33