NSLog with CGPoint data
Asked Answered
T

7

93

I have a CGPoint called point that is being assigned a touch:

UITouch *touch = [touches anyObject];

CGPoint point = [touch locationInView:self];

I want to get the x coordinate value into my console log:

NSLog(@"x: %s", point.x);

When I use this, log output for this is:

x: (null)

I have verified that point is not null when this is called using the debugger and variable watch.

Any help appreciated,

Thanks // :)

Toandfro answered 25/9, 2009 at 10:22 Comment(0)
P
261

Actually, the real easiest way to log a CGPoint is:

NSLog(@"%@", NSStringFromCGPoint(point));

The desktop Cocoa equivalent is NSStringFromPoint().

Portillo answered 25/9, 2009 at 12:7 Comment(6)
This is even better. The first answer is the easiest and lightest weight way. But this gets me both x and y from the CGPoint in one set. Nice :) Great tool :)Toandfro
Since StackOverflow saw fit to reintroduce this question in my RSS feed, I may as well pimp my general solution: jens.ayton.se/blag/almost-elegant-cave-man-debugging which allows you to go JA_DUMP(point); and get “point = { 43, 96 }” logged without having to worry about format codes.Portillo
How do I use your lib since it compiles on I386 but not on ARM? I mean, how can I work on iOS projects using it?Nicolenicolea
First, you’d need to build the FindAlignment.c file as an iOS app and run it on a device (not simulator). Then, copy the result into a new #elif block before the #else at line 172 in JAValueToString.m. If this doesn’t work, additional debugging will be required. I can’t do it since I’m not in the iOS programme.Portillo
Also worth noting that NSStringFromCGRect() exists too.Epiglottis
you can this helper to keep code clean #define CGLog(a) NSLog(@"%@", NSStringFromCGPoint(a))Honey
W
24

point.x is a floating point number, so you should use:

NSLog(@"x: %f", point.x);
Welch answered 25/9, 2009 at 10:25 Comment(0)
Y
9

The simplest way to log a CGPoint value is to use the NSValue class, since it will give you all the relevant values formatted nicely for the console. It's done like so:

NSLog(@"myPoint = %@", [NSValue valueWithCGPoint:myPoint]);

You can also use the +valueWithCGRect and +valueWithCGSize methods of NSValue when you're trying to log, say, the frame (CGRect) or size (CGSize) properties of a UIView.

Yasmeen answered 25/9, 2009 at 10:35 Comment(0)
S
2

NSLog(@"point x,y: %f,%f", point.x, point.y);

Strophanthus answered 12/11, 2010 at 11:19 Comment(0)
A
0

point.x is a floating point number so you should code like this:

NSLog(@"%@",[NSString StringWithFormat:@"%f",point.x]);
Ameliaamelie answered 6/9, 2010 at 13:40 Comment(2)
If u want to String value means u just use this!!Ameliaamelie
You're doing an unnecessary format string within a format string. Philippe's and Ahruman's approaches are much simpler and they achieve the exact same results.Exonerate
S
0

use :

NSLog(@"%@", NSStringFromCGPoint(point));

You can also use NSString for following info :

NSStringFromCGPoint
NSStringFromCGSize
NSStringFromCGRect
NSStringFromCGAffineTransform
NSStringFromUIEdgeInsets

Solus answered 22/5, 2016 at 21:22 Comment(0)
N
0

Latest syntax:

NSLog("%@", NSCoder.string(for: point!))
Nonlegal answered 5/5, 2022 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.