?
1.description方法的一般用處
1: // 指針變量的地址
2: NSLog(@"%p", &p);
3: // 對象的地址
4: NSLog(@"%p", p);
5: // <類名:對象地址>
6: NSLog(@"%@", p);
?
1: Class c = [Person class];
2:
3: // 1.會調用類的+description方法
4: // 2.拿到+description方法的返回值(NSString *)顯示到屏幕上
5: NSLog(@"%@", c);
類似于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);
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: }