“表达式不可分配"-- 在 Xcode 中将浮点数分配为其他两个浮点数的总和有问题吗? [英] "Expression is not assignable" -- Problem assigning float as sum of two other floats in Xcode?

查看:19
本文介绍了“表达式不可分配"-- 在 Xcode 中将浮点数分配为其他两个浮点数的总和有问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在钢琴应用程序中,我正在分配黑键的坐标.这是导致错误的代码行.

In a piano app, I'm assigning the coordinates of the black keys. Here is the line of code causing the error.

'blackKey' 和 'whiteKey' 都是 customViews

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

当你编写 blackKey.center.x 时,blackKey.centercenter.x 看起来都像结构成员访问,但它们'实际上是完全不同的东西.blackKey.center 是一种属性访问,它脱糖为 [blackKey center] 之类的东西,而后者又脱糖为 objc_msgSend(blackKey, @selector(center) 之类的东西)).您不能修改函数的返回值,例如 objc_msgSend(blackKey, @selector(center)).x = 2 - 它只是没有意义,因为返回值不是 存储任何有意义的地方.

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.

所以如果要修改struct,就得把属性的返回值存储在一个变量中,修改变量,然后将属性设置为新的值.

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.

这篇关于“表达式不可分配"-- 在 Xcode 中将浮点数分配为其他两个浮点数的总和有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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