码迷,mamicode.com
首页 > 其他好文 > 详细

description

时间:2015-10-24 11:26:35      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

description方法的作用是打印对象,对于一个类,如果没有重写description方法,NSLog(@“%@”,此处写类的对象),

输出的是该类的地址如下:

2015-10-24 11:00:01.211 OC_102402[1577:75844] <ClassRoom: 0x100210870>
Program ended with exit code: 0

而我们想要的效果是打印出该类的成员变量,

所以我们可以在该类里重写description方法如下:

1 -(NSString *)description{  //库方法,方法的重写
2     
3     NSString *str = [NSString stringWithFormat/*方法*/:@"我们的教室有%@的桌子,%@电脑",self/*当前方法的调用者-ClassRoom*/.desk.color,self.computer.brand];
4     
5     return str;
6 }

通过NSLog(@“%@”,此处写类的对象),会自动找到description方法,返回值是OC字符串

description

标签:

原文地址:http://www.cnblogs.com/liuguan/p/4906418.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!