Description & amp; debugDescription & amp; runtime (debug model in debug mode), describe

Source: Internet
Author: User

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-cUsed inNSLog("%@",model)This line of code is printedmodelIs 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 *)descriptionMethod. 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 containsNMultiple Attributes, possibly10, Possibly20... Is it necessarydescriptionIn 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.runtimeTechnology 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 printmodelSuch:


Write the image description here

 

DebugDescription

The problem persists ..
In the projectNSLogThere are also many statements.descriptionMethod. Many Attributes are printed on the console. It looks uncomfortable ~~ Another problem is that sometimes we don't need to printmodelSo rewrite it.descriptionMethodCounterproductiveNow! All, there is now a solution that is rewrittendebugDescriptionMethod

What isdebugDescription? ActuallydebugDescriptionAnddescriptionIs the same, but the only difference isdebugDescriptionYesXcodeUse in the consolepoCalled during command ~!

WhiledebugDescriptionIs actually called.descriptionMethod

So, in the development process andmodelDuring debugging, I recommend RewritingdebugDescriptionMethod instead of rewritingdescriptionMethod. When printingmodelIn the console.poCommand.. 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.NSLogAndpoCommand Printing


Write the image description here

Result:


Write the image description here

This achieves what we want, if you need to printmodelAnd then usepoCommand

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.