将 CGPoint 转换为字符串时出现问题

发布于 2024-09-06 20:43:34 字数 412 浏览 10 评论 0原文

我在将 CGPoint 转换为字符串时遇到问题。我尝试过各种方法,但这似乎是最有希望的但仍然行不通。有什么建议吗?

这是我的代码:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    coord = [touch locationInView:touch.view];  
    viewcoord.text = [NSString stringWithFormat:@"coordinates %@", coord.x, coord.y];

我得到输出,但它只是说“坐标(空)”,我不明白为什么......

谢谢

I have been having problems with converting a CGPoint to a string. I have tried various methods, but this seems like the most promising but it still won't work. Any suggestions?

Here is my code:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    coord = [touch locationInView:touch.view];  
    viewcoord.text = [NSString stringWithFormat:@"coordinates %@", coord.x, coord.y];

I get output but it just says "coordinates (null)" and I don't understand why...

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

捂风挽笑 2024-09-13 20:43:34
viewcoord.text = [NSString stringWithFormat:@"coordinates %@", NSStringFromCGPoint(coord)];
viewcoord.text = [NSString stringWithFormat:@"coordinates %@", NSStringFromCGPoint(coord)];
独守阴晴ぅ圆缺 2024-09-13 20:43:34

您的格式字符串使用仅适用于 Objective-C 对象的 %@ 。您看起来像是在尝试打印不是一个而是两个值(x 和 y),这两个值都是浮点数。试试这个:

viewcoord.text = [NSString stringWithFormat:@"coordinates %f, %f", coord.x, coord.y];

Your format string uses a %@ which only applies to objective-C objects. You look like you're trying to print not one but two values (the x and the y), both of which are floats. Try this:

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