这种"如果"是什么意思?

pme*_*ino 0 xcode cocoa objective-c

我一直在阅读一些关于可可的电子书,所以我可以学到更多,但我没有这么清楚这种"if"是什么意思:

if(!something)
if(something)
Run Code Online (Sandbox Code Playgroud)

而且我认为:

if (something !=nil)
Run Code Online (Sandbox Code Playgroud)

意味着如果某些东西不存在,对吧?

谢谢 :)

Reg*_*ent 8

在C中,一切都不平等NULL或被0认为是true.

从而

if (!something)
Run Code Online (Sandbox Code Playgroud)

是完全相同的

if (!(something != nil))
Run Code Online (Sandbox Code Playgroud)

因此与...相同

if (something == nil)
Run Code Online (Sandbox Code Playgroud)

在Objective-C对象的情况下(nil基本上NULL).

所以if (something)基本上是一个简短的手if (something != nil).
而且if (!something)基本上是一个简短的手if (something == nil).

对于非Objective-C指针,您只需替换nilNULL.(nil无论如何,或多或少只是语法糖.)

您也可以使用它来检查整数是否不相等0:
因此条件if ([myArray count])true除非myArray为空.