Last active
August 29, 2015 14:17
-
-
Save armadsen/cf98ceb3f4d39c5347c1 to your computer and use it in GitHub Desktop.
Tiny demo of -respondsToSelector:
This file contains 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
/* Compile and run with: | |
$> clang responds.m -ObjC -std=c99 -framework Foundation | |
$> ./a.out | |
*/ | |
#import <Foundation/Foundation.h> | |
@interface Foo : NSObject | |
- (int)bar; | |
@end | |
@implementation Foo | |
- (int)bar { return 42; } | |
@end | |
int main(int argc, char *argv[]) { | |
id foo = [Foo new]; | |
if ([foo respondsToSelector:@selector(bar)]) { | |
NSLog(@"foo responds to bar"); | |
} | |
if ([foo respondsToSelector:@selector(baz)]) { | |
NSLog(@"foo responds to baz"); | |
} | |
[foo release]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment