What values should I use for iOS boolean states?

后端 未结 5 1526
执笔经年
执笔经年 2021-01-04 10:23

It appears that in iOS I have a number of options that seem to fit for boolean values:

YES
NO
TRUE
FALSE
true
false

Which ones should I use

5条回答
  •  太阳男子
    2021-01-04 11:02

    Actually there is no difference between YES, TRUE and true those all represent a true state represented by 1.

    And NO, false, FALSE is represents a false state represented by 0.

    You can also use:

    BOOL aBool = 1;
    

    which is equivalent to BOOL aBool = true; and BOOL aBool = TRUE; and BOOL aBool = YES;

    But:

    BOOL bBool = 7;
    if (bBool)
    {
        NSLog(@"bBool is YES!\n");
    }
    if (bBool != YES) {
        NSLog("bBool is not YES!\n");
    }
    

    Will output like:

    b is YES!
    b is not YES!
    

    This is because direct comparison with YES will fail when the value of a BOOL type is a non-zero value other than 1.

    Here is a nice article for you.

提交回复
热议问题