Last active
August 29, 2015 14:01
-
-
Save SheffieldKevin/ed95d9c66e43288c4cb8 to your computer and use it in GitHub Desktop.
Calling derived class methods from the base class. What do you think this code outputs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface BaseClass : NSObject | |
+(void)callDerivedClassMethod; | |
@end | |
@interface DerivedClass : BaseClass | |
+(void)derivedClassMethod; | |
@end | |
@implementation BaseClass | |
+(void)callDerivedClassMethod | |
{ | |
SEL theSelector = NSSelectorFromString(@"derivedClassMethod"); | |
if (theSelector) | |
{ | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Warc-performSelector-leaks" | |
[self performSelector:theSelector]; | |
#pragma clang diagnostic pop | |
} | |
else | |
{ | |
NSLog(@"Didn't call derivedClassMethod."); | |
} | |
} | |
@end | |
@implementation DerivedClass | |
+(void)derivedClassMethod | |
{ | |
NSLog(@"derivedClassMethod has been called."); | |
} | |
@end | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool | |
{ | |
[DerivedClass callDerivedClassMethod]; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment