! VS在Objective-C ==零 [英] ! vs == nil in objective-c
问题描述
如果你有这样的NSString * someString的对象,有什么区别,如果有的话,
If you have an object like NSString *someString, what is the difference, if any, between
if (!someString)
VS
if (someString == nil)
谢谢!
推荐答案
第一种语法使用:
if (!someString)
利用一种的C模糊的事实,C的原标准缺乏正确的布尔类型派生的。因此,任何整数值等于0是PTED为假帧间$ P $,和从0不同的任何整数值取为真。的含义!
因此,基于这一约定和C标准的最新版本一直保持兼容原定义来定义。
exploits a sort of "ambiguity" of C deriving from the fact that the original standard of C lacked a proper boolean type. Therefore, any integer value equalling 0 was interpreted as "false", and any integer value different from "0" was taken as "true". The meaning of !
is therefore defined based on this convention and current versions of the C standard have kept the original definition for compatibility.
在特定的情况下, someString
是一个指针,所以它首先转换为整数,那么! someString
是PTED作为一个布尔值间$ P $ 真正
在 someString
点位置为0x000000
,否则evals为true。
In your specific case, someString
is a pointer, so it is first converted to an integer, then ! someString
is interpreted as a bool value of true
when someString
points at the location 0x000000
, otherwise it evals to "true".
这是在大多数情况下(我总是会说)的罚款,但在理论上, NULL /零
的可能是在一定的编译器从为0x000000
不同,所以(在极理论),这将是更好地使用第二语法,这是更明确的:
This is fine in most conditions (I would say always), but in theory, NULL/nil
could be different from 0x000000
under certain compilers, so (in very theory) it would be better to use the second syntax, which is more explicit:
if (someString == nil)
这是无论如何更具可读性和自 someString
不是一个整数(而不是一个指针),国际海事组织,一般来说更好的做法。
It is anyway more readable and since someString
is not an integer (rather a pointer), IMO, better practice in general.
编辑:关于NULL的定义...
about the definition of NULL...
无论是C标准定义NULL为0是一个有趣的话题,我...
Whether the C standard defines NULL to be 0 is an interesting topic for me...
根据 C99标准,部分7.17,通用定义
According to C99 standard, section 7.17, "Common definitions ":
NULL [它]扩展为实现定义的空指针常数;
NULL [which] expands to an implementation-defined null pointer constant;
因此,NULL被定义STDDEF.H到的实现定义的空指针常量的...
47页的同一个文档指出:
So, NULL is defined in stddef.h to an implementation-defined null pointer constant... The same document on page 47 states:
值为0,或者这样的前pression投整型常量前pression void *类型,被称为一个空指针constant.55)如果空指针常量被转换为指针类型,将所得指针,称为空指针,是保证比较不等的指针的任何对象或功能。
An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
所以,空指针常数(即(无效*)0
)可的转换的以空指针,这是保证比较不等的指针的任何对象或功能。
So, the null pointer constant (which is (void*)0
) can be converted to a null pointer and this is guaranteed to compare unequal to a pointer to any object or function.
所以,我认为基本上这取决于是否执行决定转换的结果的空指针常量的到的空指针的产生一个指向其转换回一个整数给出0目前尚不清楚,一个的空指针的PTED为整数间$ p $等于0。
So, I think that basically it depends on whether the implementation decides that the result of converting a null pointer constant to a null pointer produces a pointer which converted back to an integer gives 0. It is not clear that a null pointer interpreted as an integer equals 0.
我要说的是标准的真正尝试和执行空指针为0,但留下敞开大门的系统中的空指针不是0 。
I would say that the standard really try and enforce the null pointer being 0, but leaves the door open to systems where the null pointer was not 0.
这篇关于! VS在Objective-C ==零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!