开发者

Is multiple assignment a hack in Obj-C?

开发者 https://www.devze.com 2022-12-17 02:02 出处:网络
So, I\'ve got a class (IKImageView) with a bunch of properties. I know that view setProp: BOOL returns void. However:

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 getHasHorizont开发者_Go百科alScroller];
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...


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消