IOS Efficient Development-----Implement description method

Source: Internet
Author: User

Regardless of the development of any program, and regardless of the program of programming ape level, it will certainly be used to debug, will always print to see the object information;

The usual way is to use NSLog, for example:

NSLog (@ "obj =%@", obj)

After running, the Obj object receives the description message, and the description information returned by the method supersedes "%@" in the "format string", if obj is an array, consider the following example:

Array nsarray* obj = @[@1,@2,@3]; NSLog (@ "obj =%@", obj);//The print information is as follows obj = (       1,       2,       3       )//Dictionary nsdictionary * anotherdic = @{@ "animal": @ "dog", @ " Kid ": @" Boy "@" food ": @" bread "}; NSLog (@ "anotherdic =%@", anotherdic);//print information as follows anotherdic = {    animal = dog;    food = bread;    Kid = Boy;}

However, if you do this on a custom class, the output information is the pointer address like this

Pingktest * Object = [[Pingktest alloc] init]; NSLog (@ "object =%@", object);//Print Information//object = <PingkTest:0x7f9b5b692a30>

This information is useless compared to the information that is output by the array or the dictionary, unless the description method is implemented, and the message is printed up, calling the default method implemented by the NSObject class.

Next, let's look at the specific implementation code:

  pingktest.h//  learneffective2.0////  Created by PK on 15/5/19.//  Copyright (c) 2015 ISS. All rights reserved.//#import <Foundation/Foundation.h> @interface pingktest:nsobject@property (nonatomic,copy , readonly) NSString * name; @property (nonatomic,copy,readonly) NSString * likecolorname;-(ID) initwithname: (NSString *) Name Andcolorname: (NSString *) colorname; @end
  pingktest.m//  learneffective2.0////  Created by PK on 15/5/19.//  Copyright (c) 2015 ISS. All rights reserved.//#import "PingkTest.h" @implementation pingktest-(ID) initwithname: (NSString *) name Andcolorname :(NSString *) colorname {    if (self = [super init]) {        _name = [name copy];        _likecolorname = [colorname copy];    }    return self;} @end

This invokes the Initialize method and prints:

Pingktest * Object = [[Pingktest alloc] initwithname:@ "Pingk" andcolorname:@ "Black"]; NSLog (@ "object =%@", object);//Print Information//object = <PingkTest:0x7f9b5b692a30>

Next, implement the description method:

Implement the following code in the. m file:

-(NSString *) description{    return [NSString stringwithformat:@ "<%@:%p,\"%@,%@\ ">", [Self class],self,_name , _likecolorname];}

Run again and find the printed information magically changed, like this

Object = <pingktest:0x7ff1b8706850, "Pingk,black" >

In this way, the printed information is clearer and more meaningful to our developers;

In the new implementation of the description method, you should also print out the class name and the pointer address as the default, but the system does not print the two information to Nsarray and nsdictionary;

There is an easy way to output different information in the description method, that is, with Nsdictionary's description method, this method outputs the following information format:

Anotherdic = {    animal = dog;    food = bread;    Kid = Boy;}

Modify the description method to turn the printed information into a dictionary

-(NSString *) description{    return [NSString stringwithformat:@ "<%@:%p,%@>", [Self class],self,@{        @ " Name ": _name,        @" Likecolorname ": _likecolorname    }];}

Print the following information:

Object = <pingktest:0x7f8648f03270,{    likecolorname = black;    name = Pingk;} >

The following refer to the system provides Nsarray debugging information, see

The information printed with NSLog does not contain the class name and the pointer address, printing information with the PO command in the Debug window (console content Output window), and discovering the class name and the pointer address.

This means that there are two different ways to print an object with a PO and the Debugdescription method must be implemented. Modify the method:

-(NSString *) description{    return [nsstring stringwithformat:@ "%@", @{        @ "name": _name,        @ "Likecolorname": _likecolorname    }];} -(NSString *) debugdescription{    return [NSString stringwithformat:@ "<%@:%p,%@>",            [self class],            Self,            @{                 @ "name": _name,                 @ "Likecolorname": _likecolorname            }];}

Then insert the breakpoint, run the code to the power, you can let the information output and nsarray consistent, see:

This makes it easier for us to print out the information about the object.

The next step is to raise a new question, and if each class needs to be implemented at the time of definition, will it be troublesome?

  

IOS Efficient Development-----Implement description method

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.