Skip to content

Instantly share code, notes, and snippets.

@SheffieldKevin
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SheffieldKevin/ed95d9c66e43288c4cb8 to your computer and use it in GitHub Desktop.
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.
#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