2

When I call a certain function from my iOS app, it returns an id data type. I can't see into this function so I don't know what it's doing or what the return type really is.

If I print it to console using NSLog("@"...) I get a string similar to this: 2012-01-18 19:03:08.915 HelloWorld[165:707] Key Press State.

Is there any way for me to determine the structure of this basic Id object? How would I go about getting a specific part of that response out, such as "Key press state". String parsing seems like a horrible idea, but maybe that's the only way. Perhaps the data really is just an NSString?

Thanks!

Johan Kool
  • 15,637
  • 8
  • 64
  • 81
Jonathan
  • 1,498
  • 2
  • 20
  • 41

2 Answers2

16

Try this:

NSLog(@"Mystery object is a %@", NSStringFromClass([mysteryObject class]));
Johan Kool
  • 15,637
  • 8
  • 64
  • 81
  • Good idea. So this is telling me that the datatype is __NSCFNumber. When I try to insert this into a tablecell, I get non-ascii characters in the cells. Is there a specific conversion I should perform to get these characters into ASCII (UTF8?) – Jonathan Jan 20 '12 at 02:51
  • `__NSCFNumber` is a private implementation class of `NSNumber`. You can use `-stringValue` to get it as text. – Johan Kool Jan 20 '12 at 03:50
2

If you look in <objc/runtime.h> you'll see methods for querying an object about its class, method selectors, ivars, etc. However, you don't usually want to do that, as it breaks encapsulation and can lead to you relying on implementation details that might change in the future, so be careful with it. You can read more here.

user1118321
  • 25,567
  • 4
  • 55
  • 86