带有 CGPoint 数据的 NSLog

我有一个 CGPoint 叫做 point,它被分配了一个触点:

UITouch *touch = [touches anyObject];


CGPoint point = [touch locationInView:self];

我想把 x 坐标值输入到我的控制台日志中:

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

当我使用 this 时,它的日志输出为:

X: (无)

我已经验证了当使用调试器和变量 watch 调用这个命令时,这个点不为空。

感谢你的帮助,

谢谢//:)

43195 次浏览

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

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

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.

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

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

The desktop Cocoa equivalent is NSStringFromPoint().

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

NSLog(@"%@",[NSString StringWithFormat:@"%f",point.x]);

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

use :

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

You can also use NSString for following info :

NSStringFromCGPoint
NSStringFromCGSize
NSStringFromCGRect
NSStringFromCGAffineTransform
NSStringFromUIEdgeInsets

Latest syntax:

NSLog("%@", NSCoder.string(for: point!))