Associativity in Objective-C
I still remember my old days in HKU where Dr Choi gave us lecture on precedence and associativity. Well, I should say, luckily, I haven’t forgotten that completely
Here’s what I write in my code:
reading[idx] – 1 < 0 ? 9 : reading[idx] – 1
The compiler interprets this as: ( reading[idx] – 1 < 0 ? 9 : reading[idx] ) – 1
This is not the behaviour I want though. What I want is: reading[idx] – 1 < 0 ? 9 : (reading[idx] – 1)
You can refer to C++ Reference for reference to operator precedence and associativity.
Have fun hacking





Hi Bill,
I hope I didn’t get it wrong when I talked about it in class back then. Please send requests for refunds to the University.
In reality no one remembers the precedence of “?:” (I don’t). And what about the precedence of that “middle part”? But for the code above I’d probably do a “(d – 1) mod 10” in a “sane” language, and a “(d + 9) mod 10” otherwise (like in C).
Wish you well.
Andrew.