Last active
April 11, 2018 08:26
-
-
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.
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
#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