1.description方法的一般用处
1: // 指针变量的地址
2: NSLog(@"%p", &p);
3: // 对象的地址
4: NSLog(@"%p", p);
5: // <类名:对象地址>
6: NSLog(@"%@", p);
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
1: Class c = [Person class];
2:
3: // 1.会调用类的+description方法
4: // 2.拿到+description方法的返回值(NSString *)显示到屏幕上
5: NSLog(@"%@", c);
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
类似于Java的toString()
2、description方法的一般用处注意点
1: // 默认情况下,利用NSLog和%@输出对象时,结果是:<类名:内存地址>
2:
3: // 1.会调用对象p的-description方法
4: // 2.拿到-description方法的返回值(NSString *)显示到屏幕上
5: // 3.-description方法默认返回的是“类名+内存地址”
6: NSLog(@"%@", p);
7:
8: //Person *p2 = [[Person alloc] init];
9: //NSLog(@"%@", p2);
10:
11: //NSString *name = @"Rose";
12:
13: //NSLog(@"我的名字是%@", name);
14:
15: Person *p2 = [[Person alloc] init];
16: p2.age = 25;
17: p2.name = @"Jake";
18:
19: NSLog(@"%@", p2);
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
3、description方法的的用法
1: // 决定了实例对象的输出结果
2: //- (NSString *)description
3: //{
4: // // 下面代码会引发死循环
5: // // NSLog(@"%@", self);
6: // return [NSString stringWithFormat:@"age=%d, name=%@", _age, _name];
7: // //return @"3424324";
8: //}
9:
10: // 决定了类对象的输出结果
11: + (NSString *)description
12: {
13: return @"Abc";
14: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }