What does the question mark and the colon (?: ternary operator) mean in objective-c?
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;
Related videos on Youtube
danielreiser
Updated on March 24, 2020Comments
-
danielreiser about 4 years
What does this line of code mean?
label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;
The
?
and:
confuse me.-
Klaus Byskov Pedersen about 14 yearsIt's the ternary if-then-else operator
-
clahey about 14 yearsNote that this should be question mark, not quotation mark.
-
Tony over 12 yearsThe compiler also seems to allow
variable ?: anotherVariable
, what does this mean?' -
Scott Lahteine about 12 yearsThe ternary with no first element means the same as
(valOrVar != 0) ? valOrVar : anotherValorvar
-
-
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 over 10 yearsI 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 almost 10 yearsIt'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";
callsfirstObject
twice (iffirstObject
does not returnnil
), where[myArray firstObject] ?: @"Hello World";
produces the identical result but never callsfirstObject
more than once. -
Thang Do over 8 yearsfinally~~~~~ 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 about 8 yearsThis 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 about 8 yearsJust to clarify, it's not limited to values.
condition ? codeIfTrue : codeIfFalse
-
tomacco about 6 yearsAlso 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).