First, Introduction
Like. NET, the object object on the. NET has a ToString () method that can be used to output the object's information, and NSObject on iOS also has a method for description, which returns a description of the OBJC object, This method is called when we call NSLog to print an object or nsstring format the output of an object, and NSObject has another method debugdescription, which is used to output information in the debug console (in the console output object information such as: PO person), Debugdescription called by default when the description method
Second, the demonstration
@interface*float weight; @end @implementation Person @end
Person
Person *person =[[Person alloc] init];p erson.name=@"Bomo";p erson.age= -;p erson.weight=134; NSString*logmessage = [NSString stringWithFormat:@"%@", person]; NSLog (@"stringWithFormat:%@", LogMessage); NSLog (@"NSLog:%@", person);
By default, the Object-c object only outputs the address of the object name and object
The default implementation should look like this
-(NSString *) description{ return [nsstring stringWithFormat:@ "<%@:%p> "class"), self];}
Usually we want to see more properties of the class information, we can override the description method
@interfacePerson:nsobject@property (nonatomic, assign) Nsuinteger age; @property (nonatomic, copy) NSString*Name: @property (nonatomic, assign) Nsuinteger gender; @property (nonatomic, assign)floatweight;@end@implementation Person-(NSString *) description{return[NSString stringWithFormat:@"<person:%p> {\n\tname=%@,\n\tage=%ld,\n\tweight=%f,\n\tgender=%lu\n}", Self, self.name, self.age, Self.weight, Self.gender];}@end
Output the following information
Three, Package
In fact, we can encapsulate a common method, through the runtime API dynamically get to the object type, and output, here to create a base class Basemodel, we implement a common description method in the base class
@interfaceBasemodel:nsobject@end@implementationBasemodel-(NSString *) description1{IDModelclass = [Selfclass]; unsignedintOutcount, I; objc_property_t*properties = Class_copypropertylist (Modelclass, &outcount); Nsmutabledictionary*dict =[Nsmutabledictionary Dictionarywithcapacity:outcount]; //iterate through all the attributes Key/value for(i =0; i < Outcount; i++) {objc_property_t property=Properties[i]; NSString*propname =[NSString Stringwithutf8string:property_getname (property)]; IDValue =[[Self valueforkey:propname] description]; [Dict Setobject:value Forkey:propname]; } return[NSString stringWithFormat:@"<%@:%p>%@", Nsstringfromclass ([selfclass]), self, Dict];}-(NSString *) debugdescription{return[self description];}@end
This is our person class only need to inherit from Basemodel can implement such as nsdictionary output, here is the use of the output of the dictionary, you do not have to spell the string
There is another way to achieve this custom output through the base class method, and the method swizzling can be used to replace NSObject's original description methods.
See also: http://www.cnblogs.com/bomo/p/4693363.html
"IOS" Desctiption and Debugdescription