“表达式不可分配” -- 在 Xcode 中将浮点数指定为另外两个浮点数之和时出现问题?

发布于 2024-11-30 04:14:31 字数 179 浏览 2 评论 0原文

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

'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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜中书 2024-12-07 04:14:31

其他答案并没有准确解释这里发生的情况,所以这是基本问题:

当您编写 blackKey.center.x 时,blackKey.centercenter.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, 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.

清眉祭 2024-12-07 04:14:31

如果它是对象的属性,则不能像这样直接更改 CGPoint 的 x 值(或结构的任何值)。执行如下操作。

CGPoint _center = blackKey.center;
_center.x =  (whiteKey.frame.origin.x + whiteKey.frame.size.width);
blackKey.center = _center;

You can not directly change the x value of a CGPoint(or any value of a struct) like that, if it is an property of an object. Do something like the following.

CGPoint _center = blackKey.center;
_center.x =  (whiteKey.frame.origin.x + whiteKey.frame.size.width);
blackKey.center = _center;
乜一 2024-12-07 04:14:31
blackKey.center = CGPointMake ( whiteKey.frame.origin.x + whiteKey.frame.size.width, blackKey.center.y);

一种方法。

blackKey.center = CGPointMake ( whiteKey.frame.origin.x + whiteKey.frame.size.width, blackKey.center.y);

One way of doing it.

栖竹 2024-12-07 04:14:31

使用宏的一种替代方法:

#define CGPOINT_SETX(point, x_value) { \
    CGPoint tempPoint = point;         \
    tempPoint.x = (x_value);           \
    point = tempPoint;                 \
}

#define CGPOINT_SETY(point, y_value) { \
    CGPoint tempPoint = point;         \
    tempPoint.y = (y_value);           \
    point = tempPoint;                 \
}

CGPOINT_SETX(blackKey.center, whiteKey.frame.origin.x + whiteKey.frame.size.width);

或者稍微简单一些:

CGPOINT_SETX(blackKey.center, CGRectGetMaxX(whiteKey.frame));

One alternative using macros:

#define CGPOINT_SETX(point, x_value) { \
    CGPoint tempPoint = point;         \
    tempPoint.x = (x_value);           \
    point = tempPoint;                 \
}

#define CGPOINT_SETY(point, y_value) { \
    CGPoint tempPoint = point;         \
    tempPoint.y = (y_value);           \
    point = tempPoint;                 \
}

CGPOINT_SETX(blackKey.center, whiteKey.frame.origin.x + whiteKey.frame.size.width);

or slightly simpler:

CGPOINT_SETX(blackKey.center, CGRectGetMaxX(whiteKey.frame));
天生の放荡 2024-12-07 04:14:31

正如其含义一样,您不能为表达式赋值。例如,a + b = c 是禁止的。

As its meanings, you can't assign value to expression. For instance, a + b = c it is forbidden.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文