开发者

How to get rid of warnings when using Category in XCode

开发者 https://www.devze.com 2023-03-09 09:26 出处:网络
in my code I have created a Category over UIViewController, so that every of my UIViewControllers has a error handling method. Unfortunatey now whenever I call this method 开发者_高级运维from the cate

in my code I have created a Category over UIViewController, so that every of my UIViewControllers has a error handling method. Unfortunatey now whenever I call this method 开发者_高级运维from the category I get the following warning in XCode:

 'MainWindowViewController' may not respond to '- (...method name...):'

We try to have our code without any warnings, so I wonder if there is any clever way to keep the Category and get rid if the "may not respond to" warning".

Thanks for your help!


Importing the header where your category is declared to implementation file where methods from that category are used should eliminate that warning.


@Vladimir is right , you need to import the header file in your implementation class.

There could be one more reason for the warning you get during compilation of your code,

if you don't declare the method in header file but implement the in implementation file


Vladimir is, of course, correct. Adding a category to class makes those functions available to all instances of that class whether the header file is #imported or not. Objective-C is a dynamic language.

However - the compiler is warning you that it can't see the declaration of those messages at compile time. The code could still be valid; which is why it raises a warning rather than an error.

I like to import the category into a class that requires the extensions that the category provides. I find it a useful way of reminding me of the dependency. Some programmers, however, think that since a category provides its methods to all instances of the class, it's pointless to add it to just one class.

If you prefer not to import the category to each class that uses it, but you would like to have clean compiles - #import the category header into the pch file instead.


Try restarting XCode and cleaning. I had the same problem, was definitely including the header file but still got the warning. Restart and clean fixed it.


When everything else sensible fails to fix the problem retype the offending call on a new line just below the offending line.

You might find that the complier is happy with the new line. Delete the old line. (Things that make you go HMMM?)

I've found this happens on more than one occasion with xcode (and other editors). When all else fails type the line again.

0

精彩评论

暂无评论...
验证码 换一张
取 消