xcode 将 CGPoint x 值放在 label.text 上

发布于 2024-12-09 01:32:09 字数 573 浏览 0 评论 0原文

我尝试将 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 技术交流群。

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

发布评论

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

评论(3

冷情妓 2024-12-16 01:32:09

您正在尝试将浮点值分配给需要字符串的属性。另外,您只需要一个“=”而不是两个。试试这个:

    labelx.text = [NSString stringWithFormat:@"%f", lastPointX];

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:

    labelx.text = [NSString stringWithFormat:@"%f", lastPointX];
清晨说晚安 2024-12-16 01:32:09

您需要使用以下命令将 lastPointX 转换为字符串:

[NSString stringwithFormat:@"%d", lastPointX];

You need to convert lastPointX to string with this:

[NSString stringwithFormat:@"%d", lastPointX];
孤寂小茶 2024-12-16 01:32:09

操作数类似于 ===!=。您收到该错误的原因是您无法将 UILabel 与 CGFloat 进行比较,因此使操作数无效。我认为您将 === 打错了。因此将 == 更改为 =

接下来,您必须将lastPointX 转换为字符串,正如我所说的那样。您可以通过从 lastPointX 浮点数创建 NSString 来完成此操作。

label1.text = [NSString stringWithFormat:@"%0.0f", lastPointX];

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.

label1.text = [NSString stringWithFormat:@"%0.0f", lastPointX];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文