NSLog an object's memory address in overridden description method

Undistraction picture Undistraction · Sep 26, 2011 · Viewed 43.5k times · Source

I am overriding an object's description method. I need to know how to print the object's memory address to replace {???} in the code below:

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

I want it to print in the console like this:

<SomeClass: 0x4c05600> parameterOne: 12 parameterTwo: sausages

Answer

Vladimir picture Vladimir · Sep 26, 2011

To print address use %p format specifier and self pointer:

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