zoukankan      html  css  js  c++  java
  • NSObject协议中方法:description 和 debugDescription

    description基本概念

     

    1.NSLog(@"%@", objectA);这会自动调用objectA的description方法来输出ObjectA的描述信息.

    2.description方法默认返回对象的描述信息(默认实现是返回类名和对象的内存地址)

    3.description方法是基类NSObject 所带的方法,因为其默认实现是返回类名和对象的内存地址, 这样的话,使用NSLog输出OC对象,意义就不是很大,因为我们并不关心对象的内存地址,比较关心的是对象内部的一些成变量的值。因此,会经常重写description方法,覆盖description方法 的默认实现

    description重写的方法

    /**对象方法:当使用NSLog输出该类的实例对象的时候调用*/

    -(NSString *) description

    {

    return [NSString stringWithFormat:@"狗腿数:%d,狗眼数%d ",_legNum,_eyeNum];

    }

    /**类方法:当使用NSLog输出该类的类对象的时候调用*/

    +(NSString *) description

    {

    return @"+开头的description方法";

    }

    description陷阱

    1.千万不要在description方法中同时使用%@和self,下面的写法是错误的

    - (NSString *)description {

    return [NSString stringWithFormat:@"%@", self];

    };

    2.同时使用了%@和self,代表要调用self的description方法,因此最终会导致程序陷入死循环,循 环调用description方法;

    3.当[NSString stringWithFormat:@“%@”, self]; 使用它时,循坏调用,导致系统会发生运行时错误;

    4.当该方法使用NSLog(“%@”,self) 时候, 系统做了相关的优化,循坏调用3次后就会自动退出.

     
    @interface LaoShiEr : NSObject

    @property (nonatomic, copy, readonly) NSString *name;

    @property (nonatomic, copy, readonly) NSString *work;

    - (id)initWithName:(NSString *)name

                  work:(NSString *)work;

    @end

    @implementation LaoShiEr

    - (id)initWithName:(NSString *)name

                  work:(NSString *)work

    {

        if ((self = [superinit])) {

            _name = [name copy];

            _work = [work copy];

        }

        return self;

    }

    - (NSString *)description

    {

        return [NSStringstringWithFormat:@"<%@ : %p,"%@ %@">",[selfclass],self,_name,_work];

    }

    @end

    按照上面的代码来写,输出如下信息

    LaoShiEr *laoshi = [[LaoShiEralloc]

                            initWithName:@"laoshier"

                            work:@"coder"];

        NSLog(@"laoshier = %@",laoshi);

    laoshier = <LaoShiEr : 0xb64bec0,"laoshier coder">

    这样就比之前所输出得信息更加清楚了,也更为有用了。再description中输出很多互不相同的信息的时候可以借助NSDictionary类的description方法。修改一下老师儿的description方法

    - (NSString *)description

    {

        return [NSStringstringWithFormat:@"<%@ : %p, %@>",[selfclass],self,@{@"name":_name,@"work":_work}];

    }

    再此输出

    laoshier = <LaoShiEr : 0xb677420, {

        name = laoshier;

        work = coder;

    }>

    debugDescription方法是开发者在调试器中以控制台命令打印对象时才调用的。在NSObject类的默认实现中,此方法只是直接调用了description。以LaoShiEr为例,我们在创建实例所用的代码后面插入断点,然后通过调试器运行程序,暂停于此:并且po完成对象打印:

    <LaoShiEr : 0xb7c4310, {

        name = laoshier;

        work = coder;

    }>

    当你不想把类名与指针地址这种额外内容放在普通的描述信息里,但是却希望调试的时候能够很方便地看到它们,在此情况下,就可以使用这种输出方式来实现。

    - (NSString *)description

    {

        return [NSStringstringWithFormat:@"<%@>",@{@"name":_name,@"work":_work}];

    }

    - (NSString *)debugDescription

    {

        return [NSStringstringWithFormat:@"<%@ : %p, %@>",[selfclass],self,@{@"name":_name,@"work":_work}];

    }



     
  • 相关阅读:
    人一生要去的100个地方(世界)
    数据仓库相关书籍
    学理财要看的书籍
    数仓设计 Building the Data Warehouse
    Google Cloud 安装java
    Google Cloud install python3 (in CentOS)
    SyntaxError: Non-ASCII character 'xe5' in file test23.py on line 2, but no encoding declared;
    CentOS 安装7z
    CentOS 安装 MySQL
    复杂迭代代码分析
  • 原文地址:https://www.cnblogs.com/fengmin/p/6092678.html
Copyright © 2011-2022 走看看