How to check for True or False on Objective C BOOL variable
12,246
With BOOL
variables and properties, comparisons to YES
/NO
or TRUE
/FALSE
are unnecessary. BOOL
by itself is already a valid Boolean expression, which can go into an if
, a ternary ? :
, or a loop construct without additional comparisons.
You can write this:
// 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;
Comments
-
HurkNburkS almost 2 years
I have a NSObject that contains several objects a couple of which are BOOL variables which are set to either "T" or "F"
I would like to know how to use them in an If statement that looks like this.
if (myNSObject.firstBOOL == T) { // do some stuff here }
I cannot do this and I am not sure why, I can ask it if its TRUE or YES but not T I have looked for answers on other sites but am struggling to find anything like this so was hoping someone might be able to offer some insight.
any help would be greatly appreciated.