将CGPoint转换为字符串时遇到的问题

4

我一直在尝试将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];

我得到了输出,但它只显示“坐标(null)”,我不明白为什么...谢谢。
2个回答

14
viewcoord.text = [NSString stringWithFormat:@"coordinates %@", NSStringFromCGPoint(coord)];

1

你的格式字符串使用了%@,它只适用于 Objective-C 对象。看起来你想要打印两个值(x 和 y),它们都是浮点数。试试这个:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接