问题描述
在Xcode 6.1.1(Obj-C)
#if 1
NSLog(@ print 1 );
#endif
#if TRUE
NSLog(@ print TRUE);
#endif
#if是
NSLog(@ print YES);
#endif
结果为:
打印1
打印TRUE
能给我解释一下结果吗?为什么 #if TRUE vs #if是 vs #if 1 是不同的?
嗯...我不是一个真正的目标人物,我个人更喜欢c ++,
如果1
编译器,最后,就像计算机中的几乎所有内容一样,它们都以零和一运行。当您在代码中编写 if 1语句时,它将始终执行此操作,就像您可能在其中输入的其他所有数字一样-即零。这是因为0字节的位表示形式为 00000000,表示一个负值。
因此,在代码级和编译级中,您可以做的最基本的语句是if(此处为非零),这将始终为true。
如果为真
true是编译器中的已保存单词,最后变为1。这就是为什么if(true)始终有效的原因;自然,我认为编译器需要花费一些时间来解析它-但这几乎是唯一的区别,而且还很小。
如果是
编译器不知道单词是。因此,它会自动采用其参数,并尝试查找它之前是否已声明。当发现您之前没有在程序中定义它时,它将默认值放在if语句中-这是false;因此,该命令不会执行。
希望我帮助了:)
In Xcode 6.1.1 (Obj-C)
#if 1 NSLog(@"print 1"); #endif #if TRUE NSLog(@"print TRUE"); #endif #if YES NSLog(@"print YES"); #endif
And the result:
print 1 print TRUE
Can explain to me the result ? Why #if TRUE vs #if YES vs #if 1 are different?
Mhm... I`m not really an objective-c person, I personally like c++ better, but let me try to answer you anyways.
if 1
The compiler, in the end of it, like pretty much everything in computers, runs on zeros and ones. When you write an "if 1" statement in your code, it will always do it, like every other number you might put there - that is, except zero. That's because the bit representation of 0 in bytes is "00000000", which represends a negative value.Because of that, the most basic statement you can make to make sure you have a true, in the code level and in the compiler level, is a if(nonzero number here) - which will always be true.
if TRUE
true is a saved word in the compiler, which in the end becomes a 1. That's why if(true) always works; naturally, i assume it takes some time for the compiler to parse it - but that's pretty much the only difference, and it's fairly minor.
if YES
The compiler does not know the word "Yes". Thus, it automatically assumes its a parameter, and tries to find if it was declared before. When it finds you didnt define it before on in your program, it puts the default value in the if statement - which is, false; Thus, the command is not executted.
Hope i helped :)
这篇关于#if TRUE与#if YES与#if 1在Objective-C中是否不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!