Is there a way to make the compiler ignore this specific warning?
Here’s what I do:
UIViewController *firstViewController = AppDelegate.instance.viewController;
//open the view of the clicked subItem
if ([firstViewController respondsToSelector:@selector(openView:inView:)]) {
[firstViewController openView:subItem.itemText.text inView:activeScreen]; //warning on this line
}
I know one way that works is to change UIViewController to ViewController (Name of it’s class). But this fix won’t work in the future, so I’m just looking for a way to ignore this warning.
It won’t work in the future because, I’ll be doing something like this:
//.m
UIViewController *firstViewController;
//.h
if (someCondition) {
firstViewController = AppDelegate.instance.viewController;
}
else{
firstViewController = AppDelegate.instance.otherViewController;
}
if ([firstViewController respondsToSelector:@selector(openView:inView:)]) {
[firstViewController openView:subItem.itemText.text inView:activeScreen]; //warning on this line
}
You should cast the object to the correct type where appropriate. Note that you can ‘cast’ to a protocol if you like. This gives you the safety of knowing that required methods are implemented without having to know the concrete type.
If you want to just have the compiler not complain, it’s possible by calling
performSelector:. But then you won’t get compile-time checking.See discussion: Using -performSelector: vs. just calling the method
If you want to pass exactly one object to your selector, it’s possible by using the variant
performSelector:withObject:.If you want to pass multiple objects, you’ll have to wrap them up in a container object, as described at iOS – How to implement a performSelector with multiple arguments and with afterDelay?.