重写description方法,当使用%@时就会输出特定的字符串。
- (NSString *)description { return [NSString stringWithFormat:@"姓名:%@, 年龄:%d", _name, _age]; }
Person *person = [[Person alloc] init];
之后在使用NSLog(@"%@", person)时,就使用的是description方法。
本文介绍在Objective-C中如何重写description方法,通过%@输出特定格式的字符串,如姓名和年龄。此方法用于自定义对象的描述展示,便于调试和日志输出。
重写description方法,当使用%@时就会输出特定的字符串。
- (NSString *)description { return [NSString stringWithFormat:@"姓名:%@, 年龄:%d", _name, _age]; }
Person *person = [[Person alloc] init];
之后在使用NSLog(@"%@", person)时,就使用的是description方法。
转载于:https://www.cnblogs.com/cchHers/p/8893565.html

被折叠的 条评论
为什么被折叠?