OC description

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字符串

posted @ 2015-10-24 11:04  刘冠  阅读(249)  评论(0编辑  收藏  举报