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?
Asked
Active
Viewed 273 times
0
-
do you mean the class name ? – Teja Nandamuri May 03 '16 at 17:54
-
when you pass an object b, you want the name b to print ? – Teja Nandamuri May 03 '16 at 17:56
-
pls read the answer http://stackoverflow.com/questions/29893406/nslog-how-to-print-object-name – Teja Nandamuri May 03 '16 at 17:56
-
@TejaNandamuri That doesn't work here because the logging is being done in a location that doesn't reference the original variable. – rmaddy May 03 '16 at 17:59
1 Answers
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