Strange behaviour with NSLog
Asked Answered
L

5

13

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

Lustreware answered 29/11, 2010 at 11:9 Comment(1)
Is this something to do with the fact that frame is a cgrect which is a structure not a class?Lustreware
S
50

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

Selwyn answered 29/11, 2010 at 12:23 Comment(0)
V
10

You can also do like below

NSLog(@"%@",[NSValue valueWithCGRect:textView.frame]);
Varion answered 29/11, 2010 at 11:49 Comment(0)
M
4

UIView.frame is just a CGRect struct (which in turns contains a CGPoint and a CGSize struct). So no classes are involved, and because only classes can have a string representation, you get displayed null. What you must do if you want the values of the frame is something like this:

NSLog(@"textView.frame origin: %f %f", textView.frame.origin.x, textView.frame..origin.y);
NSLog(@"textView.frame size: %f %f", textView.frame.size.width, textView.frame.size.height);
Madagascar answered 29/11, 2010 at 11:19 Comment(0)
C
3

textview. frame will return a CGRect value. So if you want to know the frame values of a textView

NSLog(@"textView frame height: %f",textView.frame.size.height);
NSLog(@"textView frame width: %f",textView.frame.size.width);
NSLog(@"textView frame x Position: %f",textView.frame.origin.x);
NSLog(@"textView frame y Position: %f",textView.frame.origin.y);

NSLog will consider that value as null when it doesnt recognize the object what the recieved.

Campestral answered 29/11, 2010 at 11:19 Comment(0)
T
-1

In Swift it's simple:

println("\(textView.frame)")
Twilley answered 24/11, 2014 at 10:7 Comment(1)
Question is about Objective-C, not Swift.Mime

© 2022 - 2024 — McMap. All rights reserved.