“LValue”是否有效?不是我想的意思吗?
在以下代码中:
_imageView.hasHorizontalScroller = YES;
_imageView.hasVerticalScroller = YES;
_imageView.autohidesScrollers = YES;
NSLog(@"scrollbar? H %p V %p hide %p",
&(_imageView.hasHorizontalScroller),
&(_imageView.hasVerticalScroller),
&(_imageView.autohidesScrollers));
我收到错误:
Controller.m:143: error: lvalue required as unary '&' operand
Controller.m:144: error: lvalue required as unary '&' operand
Controller.m:145: error: lvalue required as unary '&' operand
请注意,我将这些变量直接用作 & 之前的左值。行...
在我毫无错误地分配给它之后,它怎么会抱怨一个值不是左值?这与目标 C 创建的神奇 getter/setter 有关吗?
我想我需要解释一些上下文来解释为什么我试图获取地址:
在我之前的SO帖子中,我展示了相同的代码,打印%d并发现在分配之后,属性由于某种原因仍然是0 。因此,我想我应该尝试获取属性的地址以查看它们的存储位置,也许我可以找出为什么我没有成功分配给它们,然后就发生了这种情况。
我认为正如人们所提到的,是的,当我进行分配时 obj-c 可能会秘密地用对 setter 的调用替换它(然后是其他一些魔法,因为在另一篇 SO 帖子中,其他人提到
BOOL b = [_imageView setHasVerticleScroller: YES]
失败了,但是
BOOL b = _imageView.hasVerticalScroller = YES;
工作正常。
In the following code:
_imageView.hasHorizontalScroller = YES;
_imageView.hasVerticalScroller = YES;
_imageView.autohidesScrollers = YES;
NSLog(@"scrollbar? H %p V %p hide %p",
&(_imageView.hasHorizontalScroller),
&(_imageView.hasVerticalScroller),
&(_imageView.autohidesScrollers));
I'm getting the error:
Controller.m:143: error: lvalue required as unary '&' operand
Controller.m:144: error: lvalue required as unary '&' operand
Controller.m:145: error: lvalue required as unary '&' operand
Notice that I am USING those variables as lvalues directly before the & lines...
How can it complain that a value isn't an lvalue right after I assign to it with no error? does this have to do with the magical getters/setters that objective C creates?
I think I need to explain some context to explain WHY I'm trying to get the address:
in my previous SO post, I showed the same code, printing %d and finding that after the assignments, the properties were still 0 for some reason. So, I figured I'd try to get the addresses of the properties to see where they're being stored and maybe I can figure out why I'm not successfully assigning to them, and then this happened.
I think that as people have mentioned, yeah, it's probably that when I do the assigment obj-c is secretly replacing that with a call to the setter (and then some other magic because in another SO post, someone else mentioned that
BOOL b = [_imageView setHasVerticleScroller: YES]
fails, but
BOOL b = _imageView.hasVerticalScroller = YES;
works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是百分百确定,但我会尝试一下答案。
这些属性都是
BOOL
类型,(我相信)这是 Objective-C 中的unsigned char
。 (也许是一个int
,我不记得了,但它是类似的东西。)所以你试图获取(&
)这些属性的地址。但您实际上并没有直接访问 ivars;而是直接访问 ivars。属性通过方法调用来获取它们的值。因此,您尝试获取方法的BOOL
返回值的地址,但由于您实际上并未将值分配给任何东西,因此没有地址 - - 你只是拥有价值。如果您这样做,您也会遇到同样的问题:
您的日志调用应如下所示:
I'm not 100% sure, but I'm going to take a stab at the answer.
Those properties are all
BOOL
types, which is (I believe) anunsigned char
in Objective-C. (Maybe anint
, I can't remember, but it's something like that.) So you're trying to take the address of (&
) those properties. But you're not actually accessing the ivars directly; properties go through a method call to get their values. So you're trying to get the address of theBOOL
return value of a method, but since you're not actually assigning the value to anything, there is no address -- you just have the value.You'd have the same problem if you did this:
Your log call should look like this:
_imageView.hasHorizontalScroller = YES;
- 在这一行中,您访问 imageView 中的属性 - 因此您实际上并不访问值,而是调用 setter 方法:在第二个示例中,您还访问属性,但是这次 getter 方法被调用并返回 BOOL。因此,作为 mipadi 点,您不需要在该 NSLog 语句中使用
&
。我建议阅读有关 obj-c 中的属性。
编辑:在这里您可以找到有关属性为何在多个赋值语句中起作用的讨论,如您提到的:
_imageView.hasHorizontalScroller = YES;
- in this line you access property in imageView - so you do not actually access value, but call setter method:In second example you also access property, but this time getter method gets called and returns BOOL. So as mipadi points you do not need to use
&
in that NSLog statement.I'd suggest to read docs about properties in obj-c.
Edit: here you can find the discussion about why properties work in multiple assignment statements like you mentioned: