Difference between ! and nil check on Objective-C object

佐手、 提交于 2019-12-11 16:19:58

问题


I'm teaching myself Objective-C 2.0.

I see from various code samples, the following two approaches to test if an object has been initialised. If that's not exactly what these tests do, please correct me.

Can you please explain the difference between the following:

if (!myObject)

and

if (myObject == nil)

回答1:


All objects are set to nil in the alloc method (or to zero for instance variables). Both of your cases checks if the object is equal to nil (is not initialized) and both will work. They are equivalent to each other. It is a matter of taste which one you prefer. Personally I tend to use

if (!myObject)

because it is my personal preference. Hope it helps!



来源:https://stackoverflow.com/questions/17696491/difference-between-and-nil-check-on-objective-c-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!