Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behaviour with NSLog

I'm using NSLog to inspect a UITextView. I have the following logging statements in my code:

    NSLog(@"textView: %@",textView);
    NSLog(@"textView.frame: %@",textView.frame);
    NSLog(@"[textView frame]: %@",[textView frame]);

And in the output to the console, i get the following:

2010-11-29 22:00:38.252 MenuMaker[57602:207] textView: <UITextView: 0x3b3afe0; frame = (0 0; 320 387); text = 'blah...'; clipsToBounds = YES; autoresize = RM+BM; layer = <CALayer: 0x3b3afa0>>
2010-11-29 22:00:38.254 MenuMaker[57602:207] textView.frame: (null)
2010-11-29 22:00:38.254 MenuMaker[57602:207] [textView frame]: (null)

The first line of output, since it contains the 'frame = (0 0; 320 387)' bit, leads me to believe that the UITextView's frame variable is all set up. But why do the next two lines show null in that case? Shouldn't they dump the frame's values?

Thanks in advance

like image 430
Chris Avatar asked Nov 29 '10 11:11

Chris


1 Answers

As the others have noted, CGRect is just a struct and, therefore, lacks a -description method (which is what the +stringWithFormat static method of NSString—used by NSLog—replaces %@ with in a format string). Therefore, you cannot pass it to NSLog directly.

However, there is no need to output any of the values individually like suggested. Cocoa provides a number of built-in methods that render several Core Graphics structures to a string:

NSStringFromCGRect()
NSStringFromCGPoint()
NSStringFromCGSize()
NSStringFromCGAffineTransform()

And so on. Therefore, outputting a CGRect in the form of a string becomes super-easy:

NSLog(@"%@", NSStringFromCGRect(rect));

You can find all these helper methods here under “String Conversions.”

like image 125
Count Chocula Avatar answered Oct 13 '22 00:10

Count Chocula