Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is multiple assignment a hack in Obj-C?

Tags:

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

like image 927
Brian Postow Avatar asked Jan 19 '10 21:01

Brian Postow


1 Answers

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.

like image 76
ken Avatar answered Oct 12 '22 12:10

ken