Objective-c – NSLog an object’s memory address in overridden description method

nslogobjective c

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

Best Answer

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

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