ios “表达式不可分配”——在 Xcode 中将浮点数指定为其他两个浮点数的总和时出现问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7074405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Expression is not assignable" -- Problem assigning float as sum of two other floats in Xcode?
提问by Mahir
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' 和 'whiteKey' 都是自定义视图
blackKey.center.x = (whiteKey.frame.origin.x + whiteKey.frame.size.width);
回答by Chuck
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 storedanywhere meaningful.
当您编写 时blackKey.center.x
,blackKey.center
和center.x
都看起来像结构成员访问,但它们实际上是完全不同的东西。blackKey.center
是一个属性访问,它脱糖到类似的东西[blackKey center]
,然后再脱糖到类似的东西objc_msgSend(blackKey, @selector(center))
。你不能修改函数的返回值,比如objc_msgSend(blackKey, @selector(center)).x = 2
——它只是没有意义,因为返回值没有存储在任何有意义的地方。
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.
所以如果你想修改结构体,你必须将属性的返回值存储在一个变量中,修改变量,然后将属性设置为新值。
回答by EmptyStack
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.
如果它是对象的属性,则不能像这样直接更改x
a CGPoint
(或结构的任何值)的值。执行以下操作。
CGPoint _center = blackKey.center;
_center.x = (whiteKey.frame.origin.x + whiteKey.frame.size.width);
blackKey.center = _center;
回答by Dair
blackKey.center = CGPointMake ( whiteKey.frame.origin.x + whiteKey.frame.size.width, blackKey.center.y);
One way of doing it.
一种方法。
回答by ThomasW
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));
回答by u9197735
As its meanings, you can't assign value to expression. For instance, a + b = c it is forbidden.
就其含义而言,您无法为表达式赋值。例如, a + b = c 是被禁止的。