iOS 高效開發-----實現description 方法

來源:互聯網
上載者:User

標籤:

無論開發什麼程式,也不管編寫程式的程式猿的水平多高 ,肯定都會用到調試,肯定會經常列印查看對象的資訊;

常用的方式就是使用NSLog,例如:

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

運行後,obj對象會收到description訊息,該方法所返回的描述資訊將取代“格式化字串”裡的“%@”,如果obj是一個數組的話,請看下面的例子:

//數組NSArray* obj = @[@1,@2,@3];NSLog(@"obj = %@",obj);//列印資訊如下obj = (       1,       2,       3       )//字典NSDictionary * anotherDic = @{@"animal":@"dog",@"kid":@"boy",@"food":@"bread"};NSLog(@"anotherDic = %@",anotherDic);//列印資訊如下anotherDic = {    animal = dog;    food = bread;    kid = boy;}

但是,如果在自訂的類上這麼做,輸出的資訊卻是指標地址 like this

PingkTest * object = [[PingkTest alloc] init];NSLog(@"object = %@",object);//列印資訊//object = <PingkTest: 0x7f9b5b692a30>

和數組或字典輸出的資訊相比 ,此資訊對我們沒什麼用,除非實現description方法,否則列印資訊時就會向上調用,調用NSObject類所實現的預設方法。

接下來,我們看一下具體的實現代碼:

////  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

此時調用初始化方法並列印:

PingkTest * object = [[PingkTest alloc] initWithName:@"pingk" andColorName:@"black"];NSLog(@"object = %@",object);//列印資訊//object = <PingkTest: 0x7f9b5b692a30>

接下來實現description方法:

在.m檔案中實現如下代碼:

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

再次運行,發現列印的資訊神奇的變了,like this

object = <PingkTest: 0x7ff1b8706850,"pingk,black">

這樣的話,列印的資訊就更加清楚了,對我們開發人員來說就更有意義了;

在新實現的description方法中,也應該像預設的那樣,列印出類名和指標地址,不過在系統對NSArray和NSDictionary就沒有將這兩項資訊列印進去;

有個簡單的方法,可以在description方法中輸出互不相同的資訊,就是藉助NSDictionary的description方法,此方法輸出的資訊格式如下:

anotherDic = {    animal = dog;    food = bread;    kid = boy;}

修改description方法,將列印的資訊變成字典

-(NSString *)description{    return [NSString stringWithFormat:@"<%@: %p,%@>",[self class],self,@{        @"name":_name,        @"likeColorName":_likeColorName    }];}

列印資訊如下:

object = <PingkTest: 0x7f8648f03270,{    likeColorName = black;    name = pingk;}>

下面參考一下系統提供得NSArray 的調試資訊,看

用NSLog 列印出的資訊並不包含類名和指標地址,在調試視窗(console內容輸出視窗中)用po 命令列印資訊,發現又有類名和指標地址 ,

這就說明是兩個不同得方法,用po 列印對象的方法就必須實現debugDescription方法。修改一下方法:

-(NSString *)description{    return [NSString stringWithFormat:@"%@",@{        @"name":_name,        @"likeColorName":_likeColorName    }];}-(NSString *)debugDescription{    return [NSString stringWithFormat:@"<%@: %p,%@>",            [self class],            self,            @{                 @"name":_name,                 @"likeColorName":_likeColorName            }];}

此時再插入斷點,運行代碼至斷電處,就能讓資訊輸出與NSArray 一致了,看:

這樣就方便我們列印查看對象的資訊了。

接下來就會引發一個新的問題,如果每個類在定義的時候都需要這樣實現,會不會覺得麻煩呢。

 

  

iOS 高效開發-----實現description 方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.