我正在重写一个对象的描述方法。我需要知道如何打印对象的内存地址来替换下面代码中的 {???}:
-(NSString *) description {
return [NSString stringWithFormat:@"<SomeClass: %@>\nparmeterOne: %@\nparameterTwo: %@",
{???}, self.parameterOne, self.paramterTwo];
}
我希望它像这样在控制台中打印:
<SomeClass: 0x4c05600> parameterOne: 12 parameterTwo: sausages
最佳答案
要打印地址,请使用 %p
格式说明符和自指针:
-(NSString *) description {
return [NSString stringWithFormat:@"<SomeClass: %p>\nparmeterOne: %@\nparameterTwo: %@",
self, self.parameterOne, self.paramterTwo];
}
https://stackoverflow.com/questions/7555143/