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 // :)

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Spanky
  • 4,980
  • 7
  • 36
  • 37

7 Answers7

261

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

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

The desktop Cocoa equivalent is NSStringFromPoint().

Jens Ayton
  • 14,532
  • 3
  • 33
  • 47
  • 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 :) – Spanky Sep 25 '09 at 18:29
  • Since StackOverflow saw fit to reintroduce this question in my RSS feed, I may as well pimp my general solution: http://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. – Jens Ayton Sep 06 '10 at 20:06
  • 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? – Dan Rosenstark Nov 22 '10 at 18:00
  • 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. – Jens Ayton Nov 23 '10 at 07:52
  • 3
    Also worth noting that NSStringFromCGRect() exists too. – djskinner Dec 08 '12 at 13:10
  • you can this helper to keep code clean #define CGLog(a) NSLog(@"%@", NSStringFromCGPoint(a)) – johndpope Mar 02 '13 at 05:52
24

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

NSLog(@"x: %f", point.x);
Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
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.

Nathan de Vries
  • 15,481
  • 4
  • 49
  • 55
2

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

Nicki
  • 984
  • 8
  • 7
0

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

NSLog(@"%@",[NSString StringWithFormat:@"%f",point.x]);
Arun
  • 3,406
  • 4
  • 30
  • 55
  • If u want to String value means u just use this!! –  Sep 07 '10 at 06:38
  • 4
    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. – Brad Larson Oct 29 '10 at 17:27
0

use :

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

You can also use NSString for following info :

NSStringFromCGPoint
NSStringFromCGSize
NSStringFromCGRect
NSStringFromCGAffineTransform
NSStringFromUIEdgeInsets

Ash
  • 5,525
  • 1
  • 40
  • 34
0

Latest syntax:

NSLog("%@", NSCoder.string(for: point!))
Edward Hasted
  • 3,201
  • 8
  • 30
  • 48