417

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.

mazniak
  • 5,849
  • 6
  • 28
  • 23

9 Answers9

806

You can try this:

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

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

jscs
  • 63,694
  • 13
  • 151
  • 195
Alex
  • 26,829
  • 3
  • 55
  • 74
  • 7
    With AppKit on OS X you would need to convert to an `NSPoint` and then call `NSStringFromPoint`. For example: `NSStringFromPoint(NSPointFromCGPoint(point))` – Alex Aug 22 '12 at 17:56
  • 19
    NSLog(@"%@", CGRectCreateDictionaryRepresentation(rect)); – Abhishek Bedi Nov 20 '12 at 10:16
  • Similar to UI, MK, CL prefixes which while all have meanings to it and need to import a respective .h file like :UIKit, MapKit, CoreLocation; Does the CG prefix mean I should import anything? If not is is just a naming convention?! – mfaani Mar 16 '16 at 20:42
236

There are a few functions like:

NSStringFromCGPoint  
NSStringFromCGSize  
NSStringFromCGRect  
NSStringFromCGAffineTransform  
NSStringFromUIEdgeInsets

An example:

NSLog(@"rect1: %@", NSStringFromCGRect(rect1));
marzapower
  • 5,531
  • 7
  • 38
  • 76
steve
  • 2,381
  • 1
  • 13
  • 2
18
NSLog(@"%@", CGRectCreateDictionaryRepresentation(rect));
Abhishek Bedi
  • 5,205
  • 2
  • 36
  • 62
13

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)
e.James
  • 116,942
  • 41
  • 177
  • 214
  • 6
    Why not just use the built in [UIKit String Conversion Functions](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/doc/uid/TP40006894-CH3-SW34)? – ma11hew28 Oct 20 '11 at 17:58
  • I do now. Those are exatly the functions that Alex and steve posted in their answers. – e.James Oct 20 '11 at 19:34
  • 1
    Would it be worth editing the answer and **adding a note** at the top "For historic interest only...". I always do that, for example http://stackoverflow.com/questions/402/iphone-app-in-landscape-mode/2530953#2530953 http://stackoverflow.com/questions/5492479/do-i-get-more-accurate-or-faster-accelerometer-readings-with-core-motion/5492892#5492892 http://stackoverflow.com/questions/4212628/whats-the-difference-between-setting-an-object-to-nil-vs-sending-it-a-release/4212826#4212826 (*"It's worth noting that this post from the previous decade, is really now only of historic interest."*) etc – Fattie Feb 21 '14 at 15:18
  • 1
    This answer is still useful, despite the existence of UIKit conversion functions, because there other structs in other frameworks, which do not have NSStringFrom helpers (eg. MKCoordinateRegion). – Greg Bell Nov 19 '14 at 22:41
10

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.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
6

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...

Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
Vaibhav Shiledar
  • 939
  • 8
  • 15
5

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.

NANNAV
  • 4,875
  • 4
  • 32
  • 50
Jens Ayton
  • 14,532
  • 3
  • 33
  • 47
  • I did that and I got compile error. Sometimes address of property expression required or something – user4951 Jun 19 '11 at 10:58
  • @Jim Thio: the macro is set up in such a way that the object being inspected must be an lvalue. (I can’t remember why; something about not being able to handle C strings properly otherwise.) In short, assign your property to a temporary variable, then call JA_DUMP on that. – Jens Ayton Jun 19 '11 at 22:11
2
NSLog(@"%@",CGRectCreateDictionaryRepresentation(rect));
UdayM
  • 1,725
  • 16
  • 34
0

Use Objective-C way to create objects from C primitives

CGRect rect = CGRectMake(1, 2, 3, 4);
CGSize size = CGSizeMake(1, 2);
NSLog(@"%@, %@",@(rect), @(size));

Output: NSRect: {{1, 2}, {3, 4}}, NSSize: {1, 2}

Consul
  • 1
  • 1