0

How can I print name of objects in Xcode? I am getting address of objects. Instead, I want names. Output is coming the address of boxes instead. How to get name of the boxes box 1 box 2?

enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
jayesh
  • 1
  • 3

1 Answers1

1

You can't. Each Box instance has no concept of the name of the variable used elsewhere to point to it. In fact, any number of different variables can reference the same Box instance so it makes no sense to attempt to print the variable name.

If you really want a name, add a name property to your Box class.

And the best way to get sane output when logging a class instance, override the description method.

Add this to your Box class in the .m file:

- (NSString *)description {
    return [NSString stringWithFormat:@"Box: h:%f x w:%f x l:%f", _height, _width, _length];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • I like your approach, I want to know, if this isn't enough or not.http://stackoverflow.com/questions/29893406/nslog-how-to-print-object-name ? – Teja Nandamuri May 03 '16 at 17:57
  • @TejaNandamuri Not in this case because there is no reference to the original variable in the code doing the logging. – rmaddy May 03 '16 at 17:58