Is multiple assignment a hack in Obj-C?

流过昼夜 提交于 2019-12-12 10:57:50

问题


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...


回答1:


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.




回答2:


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 :(



来源:https://stackoverflow.com/questions/2097339/is-multiple-assignment-a-hack-in-obj-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!