ChatGPT解决这个技术问题 Extra ChatGPT

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

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.

NSLog(@"%@", CGRectCreateDictionaryRepresentation(rect));
Try LOG_EXPR from the VTPG_Common library: vgable.com/blog/tag/log_expr

j
jscs

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


With AppKit on OS X you would need to convert to an NSPoint and then call NSStringFromPoint. For example: NSStringFromPoint(NSPointFromCGPoint(point))
NSLog(@"%@", CGRectCreateDictionaryRepresentation(rect));
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?!
m
marzapower

There are a few functions like:

NSStringFromCGPoint  
NSStringFromCGSize  
NSStringFromCGRect  
NSStringFromCGAffineTransform  
NSStringFromUIEdgeInsets

An example:

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

These are the: "single thing in all of iOS development that is most useful but least known" !! Heh
Note: for Cocoa (OS X) development, these functions don't have "CG" in the name.
A
Abhishek Bedi
NSLog(@"%@", CGRectCreateDictionaryRepresentation(rect));

e
e.James

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)

Why not just use the built in UIKit String Conversion Functions?
I do now. Those are exatly the functions that Alex and steve posted in their answers.
Would it be worth editing the answer and adding a note at the top "For historic interest only...". I always do that, for example stackoverflow.com/questions/402/iphone-app-in-landscape-mode/… stackoverflow.com/questions/5492479/… stackoverflow.com/questions/4212628/… ("It's worth noting that this post from the previous decade, is really now only of historic interest.") etc
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).
N
Nishant Tyagi

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.


N
NANNAV

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.


I did that and I got compile error. Sometimes address of property expression required or something
@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.
H
Hussain Shabbir

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


U
UdayM
NSLog(@"%@",CGRectCreateDictionaryRepresentation(rect));