Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with converting a CGPoint to a string

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

like image 863
Jason Avatar asked Dec 07 '22 02:12

Jason


2 Answers

viewcoord.text = [NSString stringWithFormat:@"coordinates %@", NSStringFromCGPoint(coord)];
like image 83
Dave DeLong Avatar answered Jan 05 '23 16:01

Dave DeLong


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];
like image 39
Ben Zotto Avatar answered Jan 05 '23 15:01

Ben Zotto