Search code examples
objective-ciosobjective-c-runtime

Get method arguments' class in Objective-C


I need to get method's argument's class at runtime.

There is a method

- (const char *)getArgumentTypeAtIndex:(NSUInteger)idx;

in NSMethodSignature, but it only returns @ if it is object. I need to get class of this object, actually detect if this object is NSArray or not. What are the possibilities to achieve that?


Solution

  • Long back I tried this.So took some time to reply for searching the code. Anyways here is the info.

    The return data of getArgumentTypeAtIndex and methodReturnType for NSMethodSignature class will be "A C string encoding the return type of the method in Objective-C type encoding." - As per docs.

    So you need to check the retuned char data and find the appropriate type by the following table.

    https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html

    I guess checking that encodings table gives you the answer!

    Happy Coding :)