多重赋值是 Obj-C 中的 hack 吗?
所以,我有一个带有一堆属性的类(IKImageView)。
我知道视图 setProp: BOOL 返回 void。然而:
BOOL b = view.prop = NO;
似乎有效。如果我有一个返回布尔值的函数 f() ,有人知道这是否真的在做:
[view setProp:f()];
Bool b = [view getProp];
或者
[view setProp: f()];
Bool b = f();
或者
BOOL TMP = f();
[view setProp: TMP];
BOOL b = TMP;
我问,因为当我这样做时:
BOOL b = view.hasHorizontalScroller = YES;
NSLog(@"b is %d scroll is %d", b, [view getHasHorizontalScroller]);
我得到“b是1,滚动是0”(这意味着 setHasHorizontalScroller 失败了出于某种原因,但 b 设置正确)
但是:
BOOL b;
[view setHasHorizontalScroller: YES];
b = [view getHasHorizontalScroller];
NSLog(@"b is %d scroll is %d", b, [view getHasHorizontalScroller]);
我得到“b is 0 滚动为 0”
这对我来说非常困惑。 (另外,如果有人能告诉我如何将属性设置为 YES 失败,但随后成功设置 b...但没有出现错误...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
。
在房产交付之前就对此进行了讨论 有些人认为这应该是一个编译错误以避免歧义。
最好完全避免施工。
It's doing
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.
看起来不是一个错误。以下代码:
产生此方法调用:
如您所见,在此链中未调用
[b text]
:(Looks like not a bug. Following code:
produces this methods calls:
As you can see
[b text]
is not called in this chain :(