Details knowledge about Objective_C NSLog

I think most of iOS app developers think that %@ is use to print the String in Objective_C, to be honest , i was think too. But , i found this opinion was wrong .
The truth is %@ is for printing objective-c objects,not just for print the NSString.To be a bit more precise. Every object is able to override

-(NSString *)description


This method is called when we use %@. It depends on the object what info of the object it will return in the NSString. So i guess iOS Foundation.framework [NSString description] was implemented like this:

-(NSString *)description {
return self;
}

So ,when we print a NSString Object , it is always print the string content.

We can also test what we found, ok let's log a UITouch instance:

2012-12-01 19:05:43.980 Touches[77547:f803] <UITouch: 0x6892770> phase: Ended tap count: 1 window: <UIWindow: 0x6b73900; frame = (0 0; 320 480); layer = <UIWindowLayer: 0x6b74540>> view: <UIView: 0x6897850; frame = (0 20; 320 460); autoresize = RM+TM; layer = <CALayer: 0x68978d0>> location in window: {238, 286} previous location in window: {238, 286} location in view: {238, 266} previous location in view: {238, 266}

See , we caught so many useful information. Great!

This knowledge point is very sample , but i was ignored for a long time ,has not been thought it till now, not good...

posted on 2012-12-01 19:13  CP0000  阅读(156)  评论(0)    收藏  举报

导航