Skip to content

Instantly share code, notes, and snippets.

@markd2
Last active April 11, 2018 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markd2/5877663 to your computer and use it in GitHub Desktop.
Save markd2/5877663 to your computer and use it in GitHub Desktop.
Support file for "Inside the Bracket, part 4" . This shows fake implementation of a method.
#import <Foundation/Foundation.h>
// clang -g -fobjc-arc -Wall -framework Foundation -o marsupial marsupial.m
@interface NSObject (AllThingsCanDance)
- (void) wombatDance: (int) repetitions;
@end
@interface Marsupial : NSObject
@end // Marsupial
@implementation Marsupial
- (NSMethodSignature *) methodSignatureForSelector: (SEL) selector {
NSMethodSignature *signature;
if (selector == @selector(wombatDance:)) {
char *typeEncodings;
asprintf(&typeEncodings, "%s%s%s%s",
@encode(void), // return
@encode(id), // self
@encode(SEL), // _cmd
@encode(int)); // repetitions
signature =
[NSMethodSignature signatureWithObjCTypes: typeEncodings];
free (typeEncodings);
} else {
signature = [super methodSignatureForSelector: selector];
}
if (signature == nil) {
NSLog (@"no signature for %@", NSStringFromSelector(selector));
}
return signature;
} // methodSignatureForSelector
- (void) forwardInvocation: (NSInvocation *) invocation {
if (invocation.selector == @selector(wombatDance:)) {
NSInteger repetitions = 0;
[invocation getArgument: &repetitions atIndex: 2];
NSLog (@"You spin me right round baby right round %ld times", repetitions);
} else {
[super forwardInvocation: invocation];
}
}
@end // Marsupial
int main (void) {
@autoreleasepool {
Marsupial *pouchy = [Marsupial new];
[pouchy wombatDance: 23];
}
return 0;
} // main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment