Objective-C newbie question. Given the following (fictional) code:
id mysteryObject = [anotherObject mysteriousMethod];
How can I determine at runtime what class mysteryObject
is?
Using - (BOOL)isKindOfClass:(Class)aClass in this case would result in TRUE both times because the inheritedClass is also a kind of the superClass. isMemberOfClass: will return NO when dealing with subclasses.
id is the generic object pointer, an Objective-C type representing "any object". An instance of any Objective-C class can be stored in an id variable.
In C language, an identifier is a combination of alphanumeric characters, i.e. first begin with a letter of the alphabet or an underline, and the remaining are letter of an alphabet, any numeric digit, or the underline.
Creating a new object in Objective-C is usually a two-step process. First, memory has to be allocated for the object, then the object is initialized with proper values. The first step is accomplished through the alloc class method, inherited from NSObject and rarely, if ever, overridden.
You can use isKindOfClass
or isMemberOfClass
For example:
if ([foo isMemberOfClass:[NSBar class]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With