Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for True or False on Objective C BOOL variable [closed]

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.

like image 801
HurkNburkS Avatar asked May 14 '13 01:05

HurkNburkS


People also ask

How do you know if Objective-C is true or false?

The if() statement is used to check for conditions. Just like we use if in normal English, if() in code is used to test for a condition—they test for the value of a boolean (or any int—in this case, a zero is considered false; any non-zero value is true).

How do you check if a boolean is true or false?

An alternative approach is to use the logical OR (||) operator. To check if a value is of boolean type, check if the value is equal to false or equal to true , e.g. if (variable === true || variable === false) . Boolean values can only be true and false , so if either condition is met, the value has a type of boolean.

Is 0 true or false in BOOL?

Like in C, the integers 0 (false) and 1 (true—in fact any nonzero integer) are used.

How do you set a BOOL variable to false?

To initialize or assign a true or false value to a Boolean variable, we use the keywords true and false. Boolean values are not actually stored in Boolean variables as the words “true” or “false”. Instead, they are stored as integers: true becomes the integer 1, and false becomes the integer 0.


1 Answers

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;
like image 161
Sergey Kalinichenko Avatar answered Nov 15 '22 08:11

Sergey Kalinichenko