PDA

View Full Version : getVariableType?




liptonlover
Sep 11, 2008, 01:45 PM
1. Will (id *)aValue accept any type of data, including C variables?

2. I need to find out what kind of variable/data that id is... how do I do that?

Nate



lee1210
Sep 11, 2008, 02:59 PM
I am 99% sure id cannot take a primitive, you'd have to wrap it in an NSNumber. Someone that's a little more familiar may want to chime in if this is incorrect, but id in my mind has to allow for type introspection, and primitives don't carry that sort of information.

Check out:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/isKindOfClass:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/isMemberOfClass:
and
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/class

They describe methods that can be called on any NSObject to get its Class or see if it is an object of a particular type. It's not that this is particularly dangerous at all, but runtime introspection (or reflection in some other languages) does require care.

-Lee

P.S. Sometimes its just a matter of knowing what to call things. In this case, it's type/runtime introspection. The wikipedia article is actually pretty good on this topic:
http://en.wikipedia.org/wiki/Introspection_(computer_science)