“表达不可分配” - 问题分配float作为xCode中的另外两个浮点数的总和? [英] "Expression is not assignable" -- Problem assigning float as sum of two other floats in xCode?
问题描述
这里是导致错误的代码行。
blackKey和whiteKey都是customViews
blackKey.center.x =(whiteKey.frame.origin.x + whiteKey.frame.size.width);
其他答案并不完全解释发生了什么在这里,所以这是基本的问题:
当你写 blackKey.center.x
时, blackKey.center
和 center.x
都看起来像结构成员访问,但它们实际上是完全不同的东西。 blackKey.center
是一个属性访问,它解析成类似于 [blackKey center]
的东西,像 objc_msgSend(blackKey,@selector(center))
。你不能修改一个函数的返回值,比如 objc_msgSend(blackKey,@selector(center))。x = 2
- 只是没有意义,因为返回值不是存储在任何有意义的地方。
因此如果你想修改结构,你必须存储属性的返回值在一个变量中,修改变量,然后将该属性设置为新的值。
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);
The other answers don't exactly explain what's going on here, so this is the basic problem:
When you write blackKey.center.x
, the blackKey.center
and center.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 like objc_msgSend(blackKey, @selector(center))
. You can't modify the return value of a function, like objc_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.
这篇关于“表达不可分配” - 问题分配float作为xCode中的另外两个浮点数的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!