How do I get class information at runtime in Objective-C?

cocoa, objective-c

Solution

If you're on Mac OS X, you can use [object className], it returns an NSString

for(id obj in array) NSLog(@"Name of the class: %@", [obj className]);

To check if it's a NSString, you should use something like this:

for(id obj in array) {
    if ([obj isKindofClass:[NSString class]]) {
        // do something
    }
}

Problem

I have NSMutableArray with different objects in it of different classes. Now I want to get the class name, related stuff and also check if the respective object is NSString or not. How should I go about it? I was trying something like the following. It wasn't working of course. ``` for(NSString *string in array){ NSLog(@"Name of the class : %@", [NSString stringWithCString:class_getName(Class id)]; ```

Original source