Is It Possible to NSLog C Structs (Like CGRect or CGPoint)?
Asked Answered
T

9

418

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.

Tropaeolin answered 15/2, 2009 at 2:28 Comment(2)
NSLog(@"%@", CGRectCreateDictionaryRepresentation(rect));Linis
Try LOG_EXPR from the VTPG_Common library: vgable.com/blog/tag/log_exprCritter
B
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).

Brunhilde answered 15/2, 2009 at 3:18 Comment(3)
With AppKit on OS X you would need to convert to an NSPoint and then call NSStringFromPoint. For example: NSStringFromPoint(NSPointFromCGPoint(point))Brunhilde
NSLog(@"%@", CGRectCreateDictionaryRepresentation(rect));Linis
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?!Manpower
G
236

There are a few functions like:

NSStringFromCGPoint  
NSStringFromCGSize  
NSStringFromCGRect  
NSStringFromCGAffineTransform  
NSStringFromUIEdgeInsets

An example:

NSLog(@"rect1: %@", NSStringFromCGRect(rect1));
Gelsemium answered 4/3, 2011 at 15:20 Comment(2)
These are the: "single thing in all of iOS development that is most useful but least known" !! HehBurford
Note: for Cocoa (OS X) development, these functions don't have "CG" in the name.Lamdin
L
18
NSLog(@"%@", CGRectCreateDictionaryRepresentation(rect));
Linis answered 20/11, 2012 at 10:17 Comment(0)
R
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)
Richers answered 15/2, 2009 at 3:50 Comment(4)
Why not just use the built in UIKit String Conversion Functions?Revenuer
I do now. Those are exatly the functions that Alex and steve posted in their answers.Richers
Would it be worth editing the answer and adding a note at the top "For historic interest only...". I always do that, for example https://mcmap.net/q/87282/-iphone-app-in-landscape-mode-2008-systems/… #5492979 #4213128 ("It's worth noting that this post from the previous decade, is really now only of historic interest.") etcBurford
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).Adorl
M
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.

Minervamines answered 8/7, 2013 at 12:33 Comment(0)
C
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...

Copalm answered 10/3, 2017 at 14:26 Comment(0)
P
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.

Puncheon answered 13/6, 2011 at 19:29 Comment(2)
I did that and I got compile error. Sometimes address of property expression required or somethingOffbeat
@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.Puncheon
E
2
NSLog(@"%@",CGRectCreateDictionaryRepresentation(rect));
Estaestablish answered 30/3, 2016 at 9:2 Comment(0)
N
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}

Numerous answered 8/11, 2022 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.