Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Argument Class / Type From Selector Variable - Objective-C

I'm working on a class that involves sending a selector as a variable to be used later. The selector is required to take one argument, a BOOL value. Executed like so:

IMP imp = [ob methodForSelector:selector];
void (*func)(id, SEL, BOOL) = (void *)imp;
func(ob, selector, YES);

If someone tries to set a selector with a variable that doesn't match a BOOL, I'd like to return an error. Is there a way, when receiving the selector that I can check whether or not its argument is a BOOL, or in general, what Class or Type of argument has been passed?

Why?

I'm setting up a quasi notification center in one of my classes so I can easily add observers and have more control over information distribution.

like image 230
Logan Avatar asked Jul 11 '26 10:07

Logan


1 Answers

Look at the method signature:

NSMethodSignature * sig = [ob methodSignatureForSelector:selector];
NSAssert(0 == strcmp(@encode(BOOL), [sig getArgumentTypeAtIndex:2]),
         @"Method must take a BOOL as its sole argument.");
like image 166
jscs Avatar answered Jul 15 '26 17:07

jscs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!