ToSelector responds with different types of arguments in the selector

Suppose we have another Object that has the same method name, but with differet argument types, such as: getMethod:(NSNumber*)aNumberand getMethod:(NSString*)aString.

How to check using responseToSelector or in another way, if the object responds to a selector with a specific type of argument, something like this:

[myObjectA respondsToSelector:@selector(getMethod:(NSNumber*))]

How do you do this? Thank.

+5
source share
2 answers

You have several ways to find argument type names for a selector. For example, this code will work:

Method method = class_getInstanceMethod([self class], @selector(someMethod:param2:param3:));
char type[256];
int argsNumber = method_getNumberOfArguments(method);
for (int i = 0; i < argsNumber; i++) {
    method_getArgumentType(method, i, type, 256);
    NSLog(@"%s", type);
}

The first and second arguments to the log are the system, and you are not interested in them, so you need other lines.

also the code below will give you the same result

NSMethodSignature *sig = [self methodSignatureForSelector:@selector(someMethod:param2:param3:)];
int args = [sig numberOfArguments];
for (int i = 0; i < args; i++) {
    NSLog(@"%s", [sig getArgumentTypeAtIndex:i]);
}

someMethod:param2:param3: may have such an implementation, for example,

- (BOOL) someMethod:(NSString *)str param2:(UIView *)view param3:(NSInteger)number
{
    return NO;
}

! , )) , const char * . , . int char, UIView NSString. id '@', id. , . , . , PLS.

, . , .

+4

, , , - :

if([myObject isKindOfClass:[A class]])
     [myObjectA getMethod:aNumber];
else if([myObject isKindOfClass:[B class]])
     [myObjectA getMethod:aString];

, , . , , , , .

0

All Articles