xcode 将 CGPoint x 值放在 label.text 上
我尝试将 lastPointX 的值放入标签文本中,但不起作用。 在 Interface Builder 中,我创建两个标签,“x”值的标签和“y”值的标签。请如果有人知道解决方案,请回答问题。
谢谢。
这是代码和错误。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
CGFloat lastPointX = lastPoint.x;
CGFloat lastPointY = lastPoint.y;
labelx.text == lastPointX; // <----- error: Semantic Issue: Invalid operands to binary expression ('NSString *' and 'CGFloat' (aka 'float'))
}
I try to put the value of lastPointX into a label text, but doesn't work.
In the Interface Builder, I create two labels, the label for "x" value and the label for "y" value. Please If anyone knows the solution, please answer the question.
Thanks.
This is the code and the error.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
CGFloat lastPointX = lastPoint.x;
CGFloat lastPointY = lastPoint.y;
labelx.text == lastPointX; // <----- error: Semantic Issue: Invalid operands to binary expression ('NSString *' and 'CGFloat' (aka 'float'))
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在尝试将浮点值分配给需要字符串的属性。另外,您只需要一个“=”而不是两个。试试这个:
You are trying to assign a float value to a property that expects a string. Also you want only a single "=" and not two. Try this:
您需要使用以下命令将 lastPointX 转换为字符串:
You need to convert lastPointX to string with this:
操作数类似于
=
、==
和!=
。您收到该错误的原因是您无法将 UILabel 与 CGFloat 进行比较,因此使操作数无效。我认为您将=
与==
打错了。因此将==
更改为=
。接下来,您必须将lastPointX 转换为字符串,正如我所说的那样。您可以通过从 lastPointX 浮点数创建 NSString 来完成此操作。
An operand is something like
=
,==
and!=
. The reason that you are getting that error is because you can't compare a UILabel to a CGFloat, therefore making the operand invalid. I think you have mistyped=
with==
. So change the==
to=
.Next, you have to convert lastPointX to a string as notme said. You can do this by creating an NSString from the lastPointX float.