How to suppress "The class may not respond to the warning" -method "with the name of the method variable?

How can I prevent this warning using the variable selector name?

NSString *methodName;

SEL method = NSSelectorFromString(methodName);

if ([self respondsToSelector:method]) {

    if ([methodName hasSuffix:@":"])
        [self method:dict];
    else
        [self method];

}
+5
source share
2 answers

Using

[self performSelector:method];

Instead

[self method];

and

[self performSelector:method withObject:dict];

Instead

[self method:dict];
+9
source

Sidyll's answer works, but there is a better solution.

Typically, you should declare a protocol:

 @protocol MyOptionalMethods
 @optional
 - (void)method:(NSDictionary*)dict;
 @end

And declare that your object conforms to the protocol:

id<MyOptionalMethods> foo;
UIView*<MyOptionalMethods> bar; // it'll be a subclass o' UIView and may implement pro to

Then check:

if ([foo respondsToSelector:@selector(method:)])
    [foo method: dict];

Thus, the compiler has the ability to completely print all the arguments. In addition, this pattern is not limited to methods that take no arguments or a single argument from an object.

, ARC ( ARC performSelector:).

+3

All Articles