What does the expression -= do?
I spent the last hour trying to fix a bug which ended up including -= in itself:
[self.myArray objectAtIndex:myButton.tag -= 1];
I’m not sure what that expression does, is it supposed to be similar to +=?
The truly mystifying part is if I NSLog() it, it borks my results:
NSLog(@"data with -=: %@", [self.myArray objectAtIndex:((UIButton *)sender).tag -= 1]);
When I comment just that line out, it works like it should. if I un-comment that line, I can’t get the index location I want. I’m not 100% sure what it does to my array, but there is no reason that I can think of that when I just log it, that it would effect other parts of the code.
Explained:
The
-=operatorlvalue -= rvalueis interpreted aslvalue = lvalue - rvalue. So, here your code can be written as:An assignment statement (
=), in turn, evaluates to its left side, so after decreasingmyButton.tagby one, it will be passed toobjectAtIndex:as if it was: