Short IF ELSE syntax in objective C Short IF ELSE syntax in objective C ios ios

Short IF ELSE syntax in objective C


Yes.

Example (pseudo):

value = (expression) ? (if true) : (if false);

Based on your example (valid code):

BOOL result = value ? YES : NO; 


It's exactly the same in both languages, except you typically don't find $ signs in Objective-C variable names.

if(value)return 1;elsereturn 0;
return value?1:0;

You should also keep in mind that the conditional operator ?: isn't a shorthand for an if-else statement so much as a shorthand for a true vs false expression. See the PHP manual.


Surprised that nobody has suggested the following :

  • Long version :

    if(value)    return 1;else    return 0;
  • Small version :

    return value;

And if value isn't a bool variable, just cast it : return (BOOL)value;


matomo