Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
i'm trying to print out a string using CGPoint locationInView, but i only receive strange numbers.

Code:
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
NSString *locationMessage = [[NSString alloc] initWithFormat:@"Location: %f", touchLocation];
locationLabel.text = locationMessage;
[locationMessage release];

how can i receive X and Y coordinates as a string?

locationLable.text (example) of the above code:
Code:
Location:  3527480165821054976.000000
 
touchLocation is a CGPoint and so won't respond correctly to %***** it in a string format. Try something like this instead:
Code:
NSString *locationMessage = [[NSString alloc] initWithFormat:@"Location: x=%f, y=%f", touchLocation.x, touchLocation.y];
 
touchLocation is a CGPoint and so won't respond correctly to %***** it in a string format. Try something like this instead:
Code:
NSString *locationMessage = [[NSString alloc] initWithFormat:@"Location: x=%f, y=%f", touchLocation.x, touchLocation.y];

ah! thanks :)... works great.

at first i tried casting the object as a string but that was crashville and it's suburbs.

thanks again.

revised code:
Code:
CGPoint touchLocation = [[touches anyObject] locationInView:self.view];
NSString *locationMessage = [[NSString alloc] initWithFormat:@"Location: X = %.0f Y = %.0f", touchLocation.x, touchLocation.y];
locationLabel.text = locationMessage;
[locationMessage release];
 
Thank you so much guys. This really helped me out.

Showing the before and after code really helped too!

-KP
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.