If I have two classes, SubClass and SuperClass:
SuperClass *super = new SuperClass();
SubClass *sub = new SubClass();
SubClass *sub_pointer;
// **The nice one-line cast below**
sub_pointer = dynamic_cast<SubClass*> super;
// Prints NO
printf("Is a subclass: %s\n", sub_pointer ? "YES" : "NO");
sub_pointer = dynamic_cast<SubClass*> sub;
// Prints YES
printf("Is a subclass: %s\n", sub_pointer ? "YES" : "NO");
I can accomplish the same thing in objective-C with isMemberOfClass as follows:
SuperClass *super = [[SuperClass alloc] init];
SubClass *sub = [[SubClass alloc] init];
SubClass *sub_pointer;
id generic_pointer;
// Not as easy:
generic_pointer = super;
if ([generic_pointer isMemberOfClass:[SubClass class]]) {
sub_pointer = generic_pointer;
} else {
sub_pointer = nil;
}
// Logs NO
NSLog(@"Is a subclass: %@", sub_pointer ? @"YES" : @"NO");
generic_pointer = sub;
if ([generic_pointer isMemberOfClass:[SubClass class]]) {
sub_pointer = generic_pointer;
} else {
sub_pointer = nil;
}
// Logs YES
NSLog(@"Is a subclass: %@", sub_pointer ? @"YES" : @"NO");
Is there an easier way than this?
(P.S. I know I don't have to use the extra id variable, but then I would have to force cast super to SubClass*, which would sometimes result in an invalid reference that I would have to clean up afterwards. That implementation, however, is less wordy, and it's below)
SuperClass *super = [[SuperClass alloc] init];
SubClass *sub = [[SubClass alloc] init];
SubClass *sub_pointer;
// Not as easy:
sub_pointer = (SubClass*) super;
if (![sub_pointer isMemberOfClass:[SubClass class]]) {
sub_pointer = nil;
}
// Logs NO
NSLog(@"Is a subclass: %@", sub_pointer ? @"YES" : @"NO");
sub_pointer = (SubClass*) sub;
if (![sub_pointer isMemberOfClass:[SubClass class]]) {
sub_pointer = nil;
}
// Logs YES
NSLog(@"Is a subclass: %@", sub_pointer ? @"YES" : @"NO");