扭转BOOL

max*_*ax_ 3 boolean objective-c

在objective-c中,如果我想反转a的值,这BOOL会工作,还是会设置BOOLto 的值NO

BOOL ab = YES;

ab = !ab; // would this reverse the BOOL, ab, and set it to NO?

if (ab == NO) {
    ab = !ab; // would this reverse the BOOL again and set it to YES?
}
Run Code Online (Sandbox Code Playgroud)

rid*_*rid 8

是的,那会有效.BOOL只是一个整数.YES1NO0.!1 == 0!0 == 1.

  • 澄清一下:从技术上讲,BOOL是一个有符号的char,而不是一个int (2认同)