Skip to content

Instantly share code, notes, and snippets.

@armadsen
Last active August 29, 2015 14:17
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 armadsen/cf98ceb3f4d39c5347c1 to your computer and use it in GitHub Desktop.
Save armadsen/cf98ceb3f4d39c5347c1 to your computer and use it in GitHub Desktop.
Tiny demo of -respondsToSelector:
/* 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