是否有可以在自定义类中重写的方法,以便在
NSLog(@"%@", myObject)
,它将打印我的对象的字段(或任何我认为重要的字段)?我想我正在寻找与Java的toString()
等价的Objective-C语言。
发布于 2009-07-09 15:49:57
它是description
实例方法,声明为:
- (NSString *)description
下面是一个示例实现(感谢grahampark):
- (NSString *)description {
return [NSString stringWithFormat: @"Photo: Name=%@ Author=%@", name, author];
}
发布于 2009-07-09 15:57:16
将此代码添加到您的照片类的@implementation
中:
- (NSString *)description {
return [NSString stringWithFormat:@"Photo: Name=%@ Author=%@",name,author];
}
发布于 2009-07-09 15:46:26
https://stackoverflow.com/questions/1104746
复制相似问题