What does the question mark and the colon (?: ternary operator) mean in objective-c?

370,111

Solution 1

This is the C ternary operator (Objective-C is a superset of C):

label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;

is semantically equivalent to

if(inPseudoEditMode) {
 label.frame = kLabelIndentedRect;
} else {
 label.frame = kLabelRect;
}

The ternary with no first element (e.g. variable ?: anotherVariable) means the same as (valOrVar != 0) ? valOrVar : anotherValOrVar

Solution 2

It's the ternary or conditional operator. It's basic form is:

condition ? valueIfTrue : valueIfFalse

Where the values will only be evaluated if they are chosen.

Solution 3

Simply, the logic would be

(condition) ? {code for YES} : {code for NO}

Solution 4

Building on Barry Wark's excellent explanation...

What is so important about the ternary operator is that it can be used in places that an if-else cannot. ie: Inside a condition or method parameter.

[NSString stringWithFormat: @"Status: %@", (statusBool ? @"Approved" : @"Rejected")]

...which is a great use for preprocessor constants:

// in your pch file...
#define statusString (statusBool ? @"Approved" : @"Rejected")

// in your m file...
[NSString stringWithFormat: @"Status: %@", statusString]

This saves you from having to use and release local variables in if-else patterns. FTW!

Solution 5

That's just the usual ternary operator. If the part before the question mark is true, it evaluates and returns the part before the colon, otherwise it evaluates and returns the part after the colon.

a?b:c

is like

if(a)
    b;
else
    c;
Share:
370,111

Related videos on Youtube

danielreiser
Author by

danielreiser

Updated on March 24, 2020

Comments

  • danielreiser
    danielreiser about 4 years

    What does this line of code mean?

    label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;
    

    The ? and : confuse me.

    • Klaus Byskov Pedersen
      Klaus Byskov Pedersen about 14 years
      It's the ternary if-then-else operator
    • clahey
      clahey about 14 years
      Note that this should be question mark, not quotation mark.
    • Tony
      Tony over 12 years
      The compiler also seems to allow variable ?: anotherVariable, what does this mean?'
    • Scott Lahteine
      Scott Lahteine about 12 years
      The ternary with no first element means the same as (valOrVar != 0) ? valOrVar : anotherValorvar
  • Bruno Bronosky
    Bruno Bronosky almost 14 years
    (update: Yuck! Reposting as an answer.) What is so important about the ternary operator is that it can be used in places that an if-else cannot. ie: Inside a condition or method parameter. [NSString stringWithFormat: @"Status: %@", (statusBool ? @"Approved" : @"Rejected")] ...which is a great use for preprocessor constants: #define statusString (statusBool ? @"Approved" : @"Rejected") ...then: [NSString stringWithFormat: @"Status: %@", statusString] This saves you from having to use and release local variables in if-else patterns. FTW!
  • Chris Hayes
    Chris Hayes over 10 years
    I appreciate that you're attempting to contribute to the site, but posting answers which simply repeat what other answers have already stated is generally discouraged, as it just clutters up questions.
  • nhgrif
    nhgrif almost 10 years
    It's probably worth mentioning that the ternary operator without the first element (?:) is even better in situations in which the left side is an expression, as the ?: prevents it from being evaluated more than once. For example: [myArray firstObject] ? [myArray firstObject] : @"Hello World"; calls firstObject twice (if firstObject does not return nil), where [myArray firstObject] ?: @"Hello World"; produces the identical result but never calls firstObject more than once.
  • Thang Do
    Thang Do over 8 years
    finally~~~~~ I have been using this operator for ages (defo my favourite) but never knew what it's called. the big question has now been answered. Thanks
  • Mike S
    Mike S about 8 years
    This is actually the best answer because the other answers imply that you can only have a "value" as a result, whereas this shows that you can put any arbitrary code as a result.
  • Mike S
    Mike S about 8 years
    Just to clarify, it's not limited to values. condition ? codeIfTrue : codeIfFalse
  • tomacco
    tomacco about 6 years
    Also want to add that the ternary operator without the first element ?: is also called the Elvis operator due to the emoji resemblance to the singer. And of course in this case as we are dropping one element, it no longer is a ternary operator but a binary operator. Binary operators include the vast majority of the operators we usually use when programming (*,+,-,^, |,||, &, >>, etc).