3 ms·
The ability to send messages to nil is not only there to prevent crashes. It can also be used to make clean APIs or to use APIs with a lot of convenience. It al
by LinaLauneBaer 13y ago
The ability to send messages to nil is not only there to prevent crashes. It can also be used to make clean APIs or to use APIs with a lot of convenience. It also gives you a lot of flexibility... It allows you to do the error handling on a case by case basis. If you are not interested in erros simply do not check for nil and go on...
Object *o = [Object new];
if(o==nil) { /*handle error and return */ return; }
[o doSomething];
vs.
Object *o = [Object new];
[o doSomething];
In the first case I catch a potential error explicitly and in the second case I simply send doSomething to nil which is perfectly fine.