[ACCEPTED]-Is It Possible to NSLog C Structs (Like CGRect or CGPoint)?-cocoa-touch

Accepted answer
Score: 806

You can try this:

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

There are a number of functions 4 provided by UIKit that convert the various CG structs into 3 NSStrings. The reason it doesn't work is because 2 %@ signifies an object. A CGPoint is a C struct (and 1 so are CGRects and CGSizes).

Score: 236

There are a few functions like:


An example:

NSLog(@"rect1: %@", NSStringFromCGRect(rect1));


Score: 18
NSLog(@"%@", CGRectCreateDictionaryRepresentation(rect));


Score: 13

I use the following macro to help me out 3 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 2 for CGPoint:

@define LogCGPoint(POINT) NSLog(@"%s: (%0.0f, %0.0f)",

Using it as follows:


Would produce 1 the following:

cgPoint: (100, 200)
Score: 10

You can use NSValue for this. An NSValue object is a simple 5 container for a single C or Objective-C 4 data item. It can hold any of the scalar 3 types such as int, float, and char, as well 2 as pointers, structures, and object ids.


  CGPoint cgPoint = CGPointMake(10,30);
    NSLog(@"%@",[NSValue valueWithCGPoint:cgPoint]);

OUTPUT : NSPoint: {10, 30}

Hope 1 it helps you.

Score: 6

Yes, you can use bellow few functions like: First 2 you have to convert CGPoint struct into 1 string, see example

1) NSStringFromCGPoint,  
2) NSStringFromCGSize,  
3) NSStringFromCGRect,  
4) NSStringFromCGAffineTransform,  
5) NSStringFromUIEdgeInsets,

For example:

1) NSLog(@"NSStringFromCGPoint = %@", NSStringFromCGRect(cgPointValue));

Like this...

Score: 5

Since Stack Overflow’s broken RSS just resurrected 2 this question for me, here’s my almost-general 1 solution: JAValueToString

This lets you write JA_DUMP(cgPoint) and get cgPoint = {0, 0} logged.

Score: 2


More Related questions