Description & debugDescription & runtime (debug model in debug mode), describe
Description
During development, many models are often used to load attributes. during development, debugging is often performed to check whether the attribute values in the model are correct.objective-c
Used inNSLog("%@",model)
This line of code is printedmodel
Is not the result we want ~! Figure:
So the question is coming again? Is there a way to solve this problem? The answer is yes ~! You only need to override- (NSString *)description
Method. The following code:
. H file
#import <Foundation/Foundation.h>@interface TestModel : NSObject@property (copy,nonatomic) NSString *text;@property (assign,nonatomic) NSInteger index;@end
. M file
#import "TestModel.h"@implementation TestModel- (NSString *)description { return [NSString stringWithFormat:@"text:%@--index:%zi",self.text,self.index];}@end
ThenNSLog("%@",model)
This line of code can print the desired results. See:
The problem persists...
If the model containsN
Multiple Attributes, possibly10
, Possibly20
... Is it necessarydescription
In the method, write the attributes one by one and splice them to return? You don't have to worry about it. It's a pain in my eyes... so we can use it.runtime
Technology to dynamically obtain attributes and return the. m file code modified as follows:
Modified. m file
# Import "TestModel. h "# import <objc/runtime. h> // import the runtime header file @ implementation TestModel-(NSString *) description {// initialize a dictionary NSMutableDictionary * dictionary = [NSMutableDictionary dictionary]; // obtain all attributes of the current class uint count; objc_property_t * properties = class_copyPropertyList ([self class], & count ); // cyclically use KVC to obtain the value of each attribute for (int I = 0; I <count; I ++) {objc_property_t property = properties [I]; NSString * name = @ (Property_getName (property); id value = [self valueForKey: name]? : @ "Nil"; // The default value is the nil string [dictionary setObject: value forKey: name]; // load it into the dictionary} // release free (properties ); // return [NSString stringWithFormat: @ "<% @: % p> -- % @", [self class], self, dictionary];} @ end
Then printmodel
Such:
Write the image description here
DebugDescription
The problem persists ..
In the projectNSLog
There are also many statements.description
Method. Many Attributes are printed on the console. It looks uncomfortable ~~ Another problem is that sometimes we don't need to printmodel
So rewrite it.description
MethodCounterproductive
Now! All, there is now a solution that is rewrittendebugDescription
Method
What isdebugDescription
? ActuallydebugDescription
Anddescription
Is the same, but the only difference isdebugDescription
YesXcode
Use in the consolepo
Called during command ~!
WhiledebugDescription
Is actually called.description
Method
So, in the development process andmodel
During debugging, I recommend RewritingdebugDescription
Method instead of rewritingdescription
Method. When printingmodel
In the console.po
Command.. M file
# Import "TestModel. h "# import <objc/runtime. h> // import the runtime header file @ implementation TestModel // rewrite debugDescription, instead of description-(NSString *) debugDescription {// declare a dictionary NSMutableDictionary * dictionary = [NSMutableDictionary]; // obtain all attributes of the current class uint count; objc_property_t * properties = class_copyPropertyList ([self class], & count ); // cyclically use KVC to obtain the value of each attribute for (int I = 0; I <count; I ++) {objc_property_t Property = properties [I]; NSString * name = @ (property_getName (property); id value = [self valueForKey: name]? : @ "Nil"; // The default value is the nil string [dictionary setObject: value forKey: name]; // load it into the dictionary} // release free (properties ); // return [NSString stringWithFormat: @ "<% @: % p> -- % @", [self class], self, dictionary];} @ end
As shown in, they are used respectively.NSLog
Andpo
Command Printing
Write the image description hereResult:
Write the image description hereThis achieves what we want, if you need to printmodel
And then usepo
Command