在这里的评论 - /sf/answers/657519691/ - 我发现BOOL从int值设置其值时有一些意外的行为.大多数情况下,如果将值设置为,0x1000则将其评估为FALSE(令人惊讶).
NSLog(@"All zero? %d %d", (BOOL)0, (bool)0);
NSLog(@"All one? %d %d %d", (BOOL)4095, (BOOL)4096, (BOOL)4097); // 4096=0x1000 or 8-bits
NSLog(@"All one? %d %d %d", (bool)4095, (bool)4096, (bool)4097);
Produces:
All zero? 0 0
All one? -1 0 1
All one? 1 1 1
Run Code Online (Sandbox Code Playgroud)
我认为这是奇怪,但还是那句话,我不从投int给BOOL多少反正.然而:
bool优先考虑BOOL?为什么或者为什么不?if (thatBool) {
或者应该更喜欢
if (thatBool ? YES : NO) {
为什么?
注意:这是这个问题的一个更具体的版本 - …