Is multiple assignment a hack in Obj-C?
-
21-09-2019 - |
Question
So, I've got a class (IKImageView) with a bunch of properties.
I know that view setProp: BOOL returns void. However:
BOOL b = view.prop = NO;
seems to work. If I had a function f() that returns a boolean, does anyone know if this is really doing:
[view setProp:f()];
Bool b = [view getProp];
or
[view setProp: f()];
Bool b = f();
or
BOOL TMP = f();
[view setProp: TMP];
BOOL b = TMP;
I ask because when I do:
BOOL b = view.hasHorizontalScroller = YES;
NSLog(@"b is %d scroll is %d", b, [view getHasHorizontalScroller]);
I get "b is 1, scroll is 0" (Which means that setHasHorizontalScroller is failing for some reason, but b is set correctly)
but:
BOOL b;
[view setHasHorizontalScroller: YES];
b = [view getHasHorizontalScroller];
NSLog(@"b is %d scroll is %d", b, [view getHasHorizontalScroller]);
I get "b is 0 scroll is 0"
This is very confusing to me. (Also, if anyone can tell me how the setting of the property to YES fails, but then it succeeds in setting b... and yet no errors come up...
Solution
It's doing
BOOL TMP = f();
[view setProp: TMP];
BOOL b = TMP;
There was discussion of this before properties shipped. Some folk thought this should be a compile error to avoid the ambiguity.
It is probably best to avoid the construction entirely.
OTHER TIPS
Looks like not a bug. Following code:
a.text = b.text = c.text;
produces this methods calls:
// [c text]
// [b setText:]
// [a setText:]
As you can see [b text]
is not called in this chain :(