Application of print function description and debugdescription in IOSI. What is description and debugdescription?
Description and Debugdescription are the two methods that are declared in the NSObject protocol, and the NSObject class implements this method, and if the subclass is not overridden, it calls the parent class's Description and Debugdescription methods. First, these two methods apply to the debugging of program code, and when we call print log, we send a message to the object.
Let's look at the Code for the Declaration section first:
+ (NSString *) description;+ (NSString *) debugdescription;
The string returned here is the information we print on the console display.
Ii. How the description method in the NSObject base class is implemented
We write the following test code:
NSObject * OBJC = [[NSObject alloc]init]; NSLog (@ "objc:%@", OBJC);
The console output information is as follows:
As you can see, the implementation of the method is roughly the same:
-(NSString *) description{return [NSString stringwithformat:@ "<%@:%p>", [Self class],&self];}
Third, rewrite the description method
Through the above introduction, we generally know the principle of the description method, in the program debugging, we can make full use of this method to bring convenience, greatly reducing the time we need to debug the program. For example: Create a test class and define two properties for it as follows:
Text.h
#import <Foundation/Foundation.h> @interface testobject:nsobject@property (nonatomic,strong) NSString * name;@ Property (Nonatomic,strong) NSString * age; @end
We will rewrite the description method in the. m file:
#import "TestObject.h" @implementation testobject-(NSString *) description{return [NSString stringwithformat:@ "%@", @{ @ "name": _name,@ "Age": _age}];} @end
The overridden method prints the properties of the test class object, and when we call the NSLog function, we print the result as follows:
is not very cool, so that we can basically not use the class name and address conversion to print data, greatly facilitates the debugging of our code.
Iv. The difference between description and debugdescription
The difference between the two methods is that the debug location is different and the different functions are called. Description is the way we call log in the program, and Debugdescription is the method we call when we debug the breakpoint at the console using the PO command, such as the method that we rewrite the test class:
-(NSString *) debugdescription{return [NSString stringwithformat:@ "<%@:%p>:%@", [self class],&self,@{@] Name ": _name,@" Age ": _age}];}
Then we add a breakpoint in the program to run, after the program is broken, we enter in the Debug area: Po text, enter after, will appear the following information:
Learn to use welcome reprint
IOS description and Debugdescription application in the debug program