在重写的描述方法中记录对象的内存地址

我正在重写对象的描述方法。我需要知道如何打印对象的内存地址,以取代 {???}的代码如下:

-(NSString *) description {
return [NSString stringWithFormat:@"<SomeClass: %@>\nparmeterOne: %@\nparameterTwo: %@",
{???}, self.parameterOne, self.paramterTwo];
}

我想让它在控制台上像这样打印:

<SomeClass: 0x4c05600> parameterOne: 12 parameterTwo: sausages
45601 次浏览

打印地址使用 %p格式说明符和自身指针:

-(NSString *) description {
return [NSString stringWithFormat:@"<SomeClass: %p>\nparmeterOne: %@\nparameterTwo: %@",
self, self.parameterOne, self.paramterTwo];
}

The easiest method is to use the super description

- (NSString *)description
{
return [NSString stringWithFormat:@"%@ Area: %@, %@", [super description], self.identifier, self.name];
}

因此,在这个模型对象是 NSObject 的子类的情况下,您可以避免额外的工作并记住 %p

手动使用 NSStringWithClass ()和% p

- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p> Area: %@, %@", NSStringFromClass([self class]), self, self.identifier, self.name];
}

So in the case of an object model in which you have a concrete implementer that is derived from this class you will show the correct class name.