Created
June 6, 2013 14:53
-
-
Save markd2/5722104 to your computer and use it in GitHub Desktop.
Play around with NSMethodSignature and @encode
This file contains hidden or 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 -Wall -framework Foundation -o signature signature.m | |
| // A class with a vararg, and non-vararg method, to see if the method signatures | |
| // are different. (don't expect them to be, but ya never know...) | |
| @interface VarBork : NSObject | |
| - (id) gronkVar: (float) hoover, ...; | |
| - (id) gronk: (float) hoover; | |
| @end // VarBork | |
| @implementation VarBork | |
| - (id) gronkVar: (float) hoover, ... { return nil; } | |
| - (id) gronk: (float) hoover { return nil; } | |
| @end // VarBork | |
| // Little utility to dump out a method signature. | |
| static void printSignature (NSMethodSignature *signature) { | |
| printf ("%ld arguments\n", [signature numberOfArguments]); | |
| for (NSUInteger i = 0; i < [signature numberOfArguments]; i++) { | |
| printf ("%ld -> %s\n", i, [signature getArgumentTypeAtIndex: i]); | |
| } | |
| printf ("returning %s\n\n", [signature methodReturnType]); | |
| } // printSignature | |
| int main (void) { | |
| NSMethodSignature *signature = | |
| [NSString instanceMethodSignatureForSelector: @selector(rangeOfCharacterFromSet:options:range:)]; | |
| printf ("rangeOfCharacterFromSet:options:range:\n"); | |
| printSignature (signature); | |
| printf ("looking at encodings\n"); | |
| printf ("int: %s\n", @encode(int)); | |
| printf ("CGRect: %s\n", @encode(CGRect)); | |
| printf ("NSString *: %s\n\n", @encode(NSString *)); | |
| // Explore varargs | |
| signature = | |
| [NSMutableString instanceMethodSignatureForSelector: @selector(appendFormat:)]; | |
| printf ("appendFormat:\n"); | |
| printSignature (signature); | |
| signature = | |
| [VarBork instanceMethodSignatureForSelector: @selector(gronk:)]; | |
| printf ("gronk:\n"); | |
| printSignature (signature); | |
| signature = | |
| [VarBork instanceMethodSignatureForSelector: @selector(gronkVar:)]; | |
| printf ("gronkVar:...\n"); | |
| printSignature (signature); | |
| return 0; | |
| } // main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment