多重赋值是 Obj-C 中的 hack 吗?

发布于 2024-08-18 14:28:12 字数 921 浏览 8 评论 0原文

所以,我有一个带有一堆属性的类(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”(这意味着 setHasHorizo​​ntalScroller 失败了出于某种原因,但 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间☆小暴躁 2024-08-25 14:28:12

BOOL TMP = f();
[view setProp: TMP];
BOOL b = TMP;

在房产交付之前就对此进行了讨论 有些人认为这应该是一个编译错误以避免歧义。

最好完全避免施工。

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.

何以心动 2024-08-25 14:28:12

看起来不是一个错误。以下代码:

a.text = b.text = c.text;

产生此方法调用:

// [c text]
// [b setText:]
// [a setText:]

如您所见,在此链中未调用 [b text] :(

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文