Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting type encodings for method signatures in Cocoa/Objective-C?

I need to construct an arbitrary NSMethodSignature with "signatureWithObjCTypes:" in Cocoa without having an object that I can ask for a signature with "methodSignatureForSelector:".

For this, I need the method encoding, which e.g. is

c12@0:4@8

for

(BOOL) isEqual: (id) object

I tried @encode(...) to obtain a type encoding, but this does not seem to work for functions (it results in an unknown type '?'). I dont't want to manually encode the function type, since this is not portable across different runtimes.

There also is no declared method to obtain the encoding from.

Is there another way of obtaining the encoding?

Regards,

Jochen

like image 228
Jochen Avatar asked Oct 08 '09 14:10

Jochen


1 Answers

What about something like:

#import <objc/runtime.h>
//inside the method implementation:
Method thisMethod = class_getClassMethod([self class], _cmd);
const char * encoding = method_getTypeEncoding(thisMethod);

Or for an arbitrary method:

#import <objc/runtime.h>
//inside the method implementation:
Method thisMethod = class_getClassMethod([self class], @selector(isEqual:));
const char * encoding = method_getTypeEncoding(thisMethod);
like image 80
Dave DeLong Avatar answered Oct 02 '22 15:10

Dave DeLong