Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Objective-C allow a successful call to null objects?

What is the use of this ( besides the non crash part ) ? I want to know in terms of thinking process behind such design.

like image 929
Agrawal Avatar asked Mar 22 '23 03:03

Agrawal


2 Answers

It's a design choice. And in Objective-C terminology, one "sends messages" to objects, instead of "calling methods". So you could say sending a message to an object that doesn't exist has simply no effect. If you address a real letter to a person that doesn't exist, no one will receive it, no one will respond or do anything.

like image 118
DrummerB Avatar answered Mar 24 '23 17:03

DrummerB


Coming from a Java background, one of the thing I found hard to "make mine", was the total absence of the famous java.lang.NullPointerException related to method invocation.

Once you get into it, you will find it is a total different approach and not a bad one also: you send a message, and if someone is not responding the program is not crashing.

This lead to enforce (in my opinion of course) to think about returning value, rather than to worry about something existing.

like image 41
Leonardo Avatar answered Mar 24 '23 18:03

Leonardo