For the more curious: conditional operators

It is not uncommon to use if and else to set the value of an instance variable. For example, you might have the following code:

i​n​t​ ​m​i​n​u​t​e​s​P​e​r​P​o​u​n​d​;​
i​f​ ​(​i​s​B​o​n​e​l​e​s​s​)​ ​{​
 ​ ​ ​ ​m​i​n​u​t​e​s​P​e​r​P​o​u​n​d​ ​=​ ​1​5​;​
}​ ​e​l​s​e​ ​{​
 ​ ​ ​ ​m​i​n​u​t​e​s​P​e​r​P​o​u​n​d​ ​=​ ​2​0​;​
}​

Whenever you have a scenario where a value is assigned to a variable based on a conditional, you have a candidate for the conditional operator, which is ?. (You will sometimes see it called the ternary operator because it takes three operands).

i​n​t​ ​m​i​n​u​t​e​s​P​e​r​P​o​u​n​d​ ​=​ ​i​s​B​o​n​e​l​e​s​s​ ​?​ ​1​5​ ​:​ ​2​0​;​

This one line is equivalent to ...

Get Objective-C Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.