Can you have an array of classes?


In chapter two, it is made clear that you cannot have an array on ints in Obj-C since all the array is really, is made up of pointers(indexes) to objects. Thus C structures are also not allowed to be added to an array. What about an array of classes? where each index of the array points to a class(a user-defined type if you will) that contains primitives or pointers to other classes/objects… ???


This worked so, I think the answer is yes.

NSMutableArray *classList = [[NSMutableArray alloc] init];
[classList addObject:NSClassFromString(@"NSDate")];
NSLog(@"Using NSDate from classList: %@", [[[classList objectAtIndex:0] alloc] init]);


Yup. Classes are objects.

You can also do [NSString class] to get a pointer to the class.