Short IF ELSE syntax in objective C

40,333

Solution 1

Yes.

Example (pseudo):

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

Based on your example (valid code):

BOOL result = value ? YES : NO; 

Solution 2

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

if(value)
return 1;
else
return 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.

Solution 3

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;

Share:
40,333
Firdous
Author by

Firdous

Updated on September 01, 2020

Comments

  • Firdous
    Firdous over 3 years

    Is there any short syntax for if-else statement in objective C like PHP:

    if($value)
    return 1;
    else
    return 0;
    

    shorter version:

    return $value?1:0;
    
    • Bryan Chen
      Bryan Chen over 10 years
      return value?1:0; is same as return !!value;
    • Hot Licks
      Hot Licks over 10 years
      No, there is no such thing in Objective-C itself. But Objective-C is built on C and has full access to C syntax/semantics, and the exact same syntax is present in C, C++, Java, and several other languages. PHP no doubt got the construct from C.
    • Regexident
      Regexident over 9 years
      FYI, the ? :-thing is called a ternary operator.
    • holex
      holex over 9 years
      as long as the PHP is based on syntax of ANSI-C-language, and the ObjectiveC is a subset of the ANSI-C-language, and the ternary operator is available in ANSI-C already – this question is totally stupid and would not have deserved even a single upvote.
    • Firdous
      Firdous over 9 years
      @holex demand my friend!
    • holex
      holex over 9 years
      @Firdous, that 'demand' is based on that fact, where most of the developers have no idea about the languages / operating systems and they are not interested in learning more about those either – and that is really good in my view, because as long as such question can be raised and upvoted 19 times(!) by a strict developer community, none of the companies will question the professionals' rates; they simply demand them and such questions just push our hourly rate up to the sky; it is brilliant, isn't it? :)
    • Firdous
      Firdous over 9 years
      @holex so how many hourly rate u managed to get after this question? :)
    • holex
      holex over 9 years
      @Firdous, here I'm doing it for fun and free; contact me in private if you have a particular task to commit it and I will tell my rate to you. ;)
  • atomkirk
    atomkirk about 10 years
    ?: is available in C and kids. It means "this or that if this is false". int i = j ?: k; If j is 0, k will be assigned.
  • BoltClock
    BoltClock about 10 years
    @atomkirk: Interesting, so it does exist outside of PHP (and works identically). I removed my erroneous statement.
  • Regexident
    Regexident over 9 years
    That's because the latter (small version) is just wrong. For old ObjC runtimes and compilers at least. On those BOOL is a typedef on signed char. As such any value with a value larger than 255 with the smaller byte all zeros (such as 256) would result in NO, instead of YES, as casting to a single-byte signed char would strip the higher, non-zero bits away. Do return !!value; instead, or return value != NO;. More: nshipster.com/bool
  • siburb
    siburb over 9 years
    The other benefit of ?: is that "value" is only evaluated once if for example you want to do something like: "if value return value else return nil"
  • Sergey Lost
    Sergey Lost almost 9 years
    This makes my code so nice, tidy and sophisticated. Thanks!
  • Septronic
    Septronic about 8 years
    I used it like this and worked perfectly: NSString *toRet = [dataToSave writeToFile:filePath atomically:NO]? @"Yes" : @"No";