“表达式不可分配” -- 在 Xcode 中将浮点数指定为另外两个浮点数之和时出现问题?
在钢琴应用程序中,我分配黑键的坐标。 这是导致错误的代码行。
'blackKey' 和 'whiteKey' 都是自定义视图
blackKey.center.x = (whiteKey.frame.origin.x + whiteKey.frame.size.width);
In a piano app, I'm assigning the coordinates of the black keys.
Here is the line of code causing the error.
'blackKey' and 'whiteKey' are both customViews
blackKey.center.x = (whiteKey.frame.origin.x + whiteKey.frame.size.width);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
其他答案并没有准确解释这里发生的情况,所以这是基本问题:
当您编写
blackKey.center.x
时,blackKey.center
和center.x
看起来都像结构成员访问,但实际上它们是完全不同的东西。blackKey.center
是一个属性访问,它脱糖为[blackKey center]
之类的内容,而后者又脱糖为objc_msgSend(blackKey, @selector(center) 之类的内容))
。您无法修改函数的返回值,例如 objc_msgSend(blackKey, @selector(center)).x = 2 - 它没有意义,因为返回值不是 < em>存储任何有意义的地方。因此,如果要修改结构体,则必须将属性的返回值存储在变量中,修改变量,然后将属性设置为新值。
The other answers don't exactly explain what's going on here, so this is the basic problem:
When you write
blackKey.center.x
, theblackKey.center
andcenter.x
both look like struct member accesses, but they're actually completely different things.blackKey.center
is a property access, which desugars to something like[blackKey center]
, which in turn desugars to something likeobjc_msgSend(blackKey, @selector(center))
. You can't modify the return value of a function, likeobjc_msgSend(blackKey, @selector(center)).x = 2
— it just isn't meaningful, because the return value isn't stored anywhere meaningful.So if you want to modify the struct, you have to store the return value of the property in a variable, modify the variable, and then set the property to the new value.
如果它是对象的属性,则不能像这样直接更改 CGPoint 的 x 值(或结构的任何值)。执行如下操作。
You can not directly change the
x
value of aCGPoint
(or any value of a struct) like that, if it is an property of an object. Do something like the following.一种方法。
One way of doing it.
使用宏的一种替代方法:
或者稍微简单一些:
One alternative using macros:
or slightly simpler:
正如其含义一样,您不能为表达式赋值。例如,a + b = c 是禁止的。
As its meanings, you can't assign value to expression. For instance, a + b = c it is forbidden.