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

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


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


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.


Simply, the logic would be

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