I want to be able to debug C structures without having to explicitly type every property that they consist of.
i.e. I want to be able to do something like this:
CGPoint cgPoint = CGPointMake(0,0);
NSLog(@"%@",cgPoint);
Obviously the '%@' won't work, hence the question.
You can try this:
NSLog(@"%@", NSStringFromCGPoint(cgPoint));
There are a number of functions provided by UIKit that convert the various CG structs into NSString
s. The reason it doesn't work is because %@
signifies an object. A CGPoint
is a C struct (and so are CGRect
s and CGSize
s).
There are a few functions like:
NSStringFromCGPoint
NSStringFromCGSize
NSStringFromCGRect
NSStringFromCGAffineTransform
NSStringFromUIEdgeInsets
An example:
NSLog(@"rect1: %@", NSStringFromCGRect(rect1));
NSLog(@"%@", CGRectCreateDictionaryRepresentation(rect));
I use the following macro to help me out with NSRect:
#define LogRect(RECT) NSLog(@"%s: (%0.0f, %0.0f) %0.0f x %0.0f",
#RECT, RECT.origin.x, RECT.origin.y, RECT.size.width, RECT.size.height)
You could do something similar for CGPoint:
@define LogCGPoint(POINT) NSLog(@"%s: (%0.0f, %0.0f)",
#POINT POINT.x, POINT.y);
Using it as follows:
LogCGPoint(cgPoint);
Would produce the following:
cgPoint: (100, 200)
You can use NSValue
for this. An NSValue object is a simple container for a single C or Objective-C data item. It can hold any of the scalar types such as int, float, and char, as well as pointers, structures, and object ids.
Example:
CGPoint cgPoint = CGPointMake(10,30);
NSLog(@"%@",[NSValue valueWithCGPoint:cgPoint]);
OUTPUT : NSPoint: {10, 30}
Hope it helps you.
Since Stack Overflow’s broken RSS just resurrected this question for me, here’s my almost-general solution: JAValueToString
This lets you write JA_DUMP(cgPoint)
and get cgPoint = {0, 0}
logged.
Yes, you can use bellow few functions like: First you have to convert CGPoint struct into string, see example
1) NSStringFromCGPoint,
2) NSStringFromCGSize,
3) NSStringFromCGRect,
4) NSStringFromCGAffineTransform,
5) NSStringFromUIEdgeInsets,
For example:
1) NSLog(@"NSStringFromCGPoint = %@", NSStringFromCGRect(cgPointValue));
Like this...
NSLog(@"%@",CGRectCreateDictionaryRepresentation(rect));
Success story sharing
NSPoint
and then callNSStringFromPoint
. For example:NSStringFromPoint(NSPointFromCGPoint(point))