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.
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).
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.
Like in C, the integers 0 (false) and 1 (true—in fact any nonzero integer) are used.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With