如何在Objective C BOOL变量上检查True或False

Hur*_*rkS 6 boolean objective-c ios

我有一个NSObject包含几个对象,其中两个是BOOL变量,设置为"T"或"F"

我想知道如何在看起来像这样的If语句中使用它们.

if (myNSObject.firstBOOL == T) {
    // do some stuff here
}
Run Code Online (Sandbox Code Playgroud)

我不能这样做,我不知道为什么,我可以问它是否为TRUEYES但不是T 我在其他网站上寻找答案但是我很难找到这样的东西所以希望有人能够提供一些见解.

任何帮助将不胜感激.

das*_*ght 9

对于BOOL变量和属性,与YES/ NOTRUE/的比较FALSE是不必要的.BOOL它本身已经是一个有效的布尔表达式,它可以进入if一个三元组? :或一个循环结构,而无需进行额外的比较.

你可以这样写:

// Use a BOOL in an if
if (myObj.boolPropertyOne) {
    // This will execute if boolPropertyOne is set to True
}
// Use a BOOL in a loop
while (!myObj.boolPropertyTwo) {
    // This will execute while boolPropertyTwo is set to False
}
// Use a BOOL in a conditional expression
int res = myObj.boolPropertyThree ? 18 : 42;
Run Code Online (Sandbox Code Playgroud)