“表达不可分配” - 问题分配float作为xCode中的另外两个浮点数的总和? [英] "Expression is not assignable" -- Problem assigning float as sum of two other floats in xCode?

查看:153
本文介绍了“表达不可分配” - 问题分配float作为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆