Chris
Chris

Reputation: 40613

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

Upvotes: 13

Views: 8165

Answers (5)

Segev
Segev

Reputation: 1287

In Swift it's simple:

println("\(textView.frame)")

Upvotes: -1

ipraba
ipraba

Reputation: 16543

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.

Upvotes: 3

Count Chocula
Count Chocula

Reputation: 1041

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

Upvotes: 50

Iphone_bharat
Iphone_bharat

Reputation: 1159

You can also do like below

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

Upvotes: 10

slup
slup

Reputation: 5534

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

Upvotes: 4

Related Questions