1

Can anybody tell me why the following was okay:

if (newEditingMode) {
    dayView.dayViewIsInEditingMode = YES;
}
else{
    dayView.dayViewIsInEditingMode = NO;
}

But the following gave the error Expression is not assignable:

newEditingMode ? dayView.dayViewIsInEditingMode = YES : dayView.dayViewIsInEditingMode = NO;
2
  • 1
    You are not supposed to perform assignments in the ternary operator. The ternary operator should be: <variable> = <condition> ? <true value> : <false value>. The result of the whole operator will be either the true or false value.
    – rmaddy
    Apr 27, 2013 at 18:56
  • @rmaddy Yes, seems like the brackets allowed it compile but both Metabble and rickyman gave the code I needed in their answer. Thanks Apr 27, 2013 at 19:05

2 Answers 2

4

You can use parentheses and it will compile. Try the following.

newEditingMode ? (dayView.dayViewIsInEditingMode = YES) : (dayView.dayViewIsInEditingMode = NO);

However, you shouldn't perform assignment inside the ternary conditional itself; you should use this instead.

dayView.dayViewIsInEditingMode = newEditingMode ? YES : NO;

Or, in this case, you can just drop the ternary altogether, since it is redundant.

dayView.dayViewIsInEditingMode = newEditingMode;
6
  • Yes. Thanks very much. That worked. Will accept your answer when SO lets me. Apr 27, 2013 at 18:46
  • 1
    None of the parentheses are needed. The first line of code in this answer is incorrect. Use either the 2nd line of code (without the parens) or better yet, use the 3rd line.
    – rmaddy
    Apr 27, 2013 at 18:58
  • @rmaddy I'm not sure if you are saying that the first line is incorrect because it is not the correct way of performing the task at hand, in which case I agree, or that you can get it to compile without parentheses. Clarify? Also, while some parentheses aren't needed, such as the ones on the second line and the first pair of the first line, I always use them on all three parts for consistency. I would appreciate if you would clarify exactly what you think should change with the lines above. :)
    – Metabble
    Apr 27, 2013 at 19:04
  • I'm saying that the first line is flat out inappropriate - parentheses or not. The parens may allow it to compile but it's poorly written code. Certainly the parens around newEditingMode is pointless.
    – rmaddy
    Apr 27, 2013 at 19:21
  • @rmaddy In that case, I agree. I should've made it more explicit that the first line shouldn't be used. I will edit this post accordingly, as well as remove extraneous parentheses. I didn't really think of whether or not they were necessary, I simply placed them out of (bad) habit. Thanks for taking the time to point that out.
    – Metabble
    Apr 27, 2013 at 19:28
3

The ternary operator does very strange things to the order of operations. Here is a posible solution which might help.

dayView.dayViewIsInEditingMode = (newEditingMode ? YES : NO);

Seeing what you are doing, you could alternatively write it like so:

dayView.dayViewIsInEditingMode = newEditingMode;
2
  • Thanks, you are correct. Metabble got there first though so I accepted his answer. Apr 27, 2013 at 18:48
  • +1. It was very close, and you actually pointed out the alternative solutions whilst I was editing mine to add them.
    – Metabble
    Apr 27, 2013 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.