How to get rid of warnings when using Category in XCode - iphone

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.

Related

Why does XCode warn me that a method in another class might not exist when it does?

This is more of an annoyance since the code works fine but maybe there's something here that I should be doing differently. At any rate, all my calls to methods in a class that's different from the caller are getting flagged as "No '-foo' method found" or "'ClassA' may not respond to '-bar'" by XCode. In reality the methods do exist and I've imported the associated header (that contains the method definition) so XCode should know what's going on. Ideas?
This happens when Xcode does not know about the method you are trying to call.
This can be due to a few reasons:
There is no method declaration for that method (either in the header file, or earlier in the .m file.
There is no method declaration for the method and the method is below the one you are trying to call it in.
You are not importing a header file you need.
Make sure you didn't misspell the method name (or used the wrong case, wrong parameter list, wrong parameter order, wrong parameter types, etc.) in the header. Make sure you really imported the header before the code producing the error.

Fix an error in an objective c tutorial

I'm on this step of the "Your First iOS Application" tutorial from Apple.
However, the line [self setMyViewController:aViewController]; has an error and the app appears as a blank black screen. The error message is 'HelloWorldAppDelegate' may not respond to '-setMyViewController' (2)
I've been following the tutorial carefully. How can I make this error go away? Why is it so ambiguous (it "may" not respond? under what circumstances will it?) and why am I getting this error in the first place? What step did I miss?
Go to your header-file and add - (void)setMyViewController:(UIViewController *)viewController;
Maybe you need to rename the parameter.
And btw. it's a warning not an error. Warnings are just for signalize that it could have problems if you haven't included the method.
If you declare a method in your .h file, the .m file gives out a warning if it isn't included. Don't worry it's not wrong if you forgot to declare a method in your .h file.
Are you running your code right after following that step? You should hold off doing so until you have finished following all steps on the page.
The Housekeeping section lists a step that you have to follow in order to make your property setter work:
In the #implementation block of the class, tell the compiler to synthesize the accessor methods for the view controller:
#synthesize myViewController;
After you finish adding the code as described by that section, running your code should work. If not, check your imports and your HelloWorldAppDelegate.h file — you may have missed something else too like declaring your #property.
Why is it so ambiguous (it "may" not respond? under what circumstances will it?)
It will respond if the compiler can find an implementation of the method, and the method is declared in the header file (unless it's a property accessor), and work as normal. If it does not, your program crashes.
and why am I getting this error in the first place?
You typically get that warning (it's not an error) if the property is not synthesized. By synthesizing it, the compiler creates the myViewController and setMyViewController: accessor methods in order for your code to access that property.
That should be a warning not an error.
Take heed of warnings, and you're right to want to eliminate them, but the code will compile and may run with warnings.
The warning could be occurring because there isn't a prototype for the method setMyViewController before it is referenced. The prototype is usually defined in the associated header '.h' file.
The prototype looks like the entry line of the method, up to, but not including the first '{', and with a ';' on the end.
Adding a prototype allows the compiler to verify you're calling the method correctly (and just as importantly, you typed it correctly ;-)

Why is XCode freaking out?? I.e., Xcode says methods & properties do not exist but they do!

XCode has been acting really, really strange recently. It is telling me that various classes' methods and properties do not exist - but they do! This is happening both with a custom class, and a Core Data class. I have declared all of the methods and properties, including all the necessary #synthesize calls, and have predeclared the classes using #class in the files which use them and included the .h files, but when I try to access the methods & properties - it throws errors or warnings, along the lines "No '+newMatrix' method found", "'Collection' may not respond to '+newMatrix'", and "Request for member 'isLanguage' in something not a structure or a union." These have all be declared properly - what could be causing XCode to choke?
Check whether they are in the list of files being compiled. You might not have added the files.
For Core data did you include the Framework?
Make sure you're using the right import statements - these two are very different:
#import <SDKLibrary.h>
#import "CustomClass.h"

Error: Expected specifier-qualifier-list before ... in XCode Core Data

I keep on getting this error "error: expected specifier-qualifier-list for core data code im working with, in the app delegate.
Now when i get this error and about 40 other errors relating to managedobjectcontext etc, i thought maybe the library needs to be imported. Now i havent done this before, but i went to Frameworks group and add existing frameworks and it added CoreData.framework. I re-build and it still came up with the error. Do I need to import anything in the headers explicitly or is there some other step i need to do?
Thanks
Can you specify the exact error? The bit after "for" is probably the important part.
The error expected specifier-qualifier-list... indicates that you've tried to use an undefined data type in a member declaration, either in a class or a struct. You need to #import the specific header where the missing type is defined. To find out which header contains the missing type, Command-double-click on the type name to open the header in which it's defined. Next, hold down the Command key and click on the title of the window to see the header's full path. For example, command-double-clicking on "NSImage" opens up the NSImage.h file, and command-clicking the title shows that it's in AppKit.framework. So the import statement for this header would be #import <AppKit/NSImage.h>.
You're right, but it's...
#import <CoreData/CoreData.h>

Apple's Singleton example

Used the MyGizmoClass example in a iPhone app where I have an object that sets and maintain db info.
How do I eliminate the
'MyGizmoClass' may not respond to '+sharedManager'
Warning
The offending line of code is:
NSString *databasePath = [[MyGizmoClass sharedManager]databasePath];
It sounds like the +sharedManager method is not declared in the header. You've mentioned importing the header a couple of times but haven't said whether +sharedManager is part of that header. The error you're seeing indicates that either (a) the header's not being imported (and you've said that it is) or (b) the header is being imported but doesn't contain the method in question.
Not sure if this belonged in a comment or as and answer.
MyGizmoClass.h is imported in implementation file (and in it's header (just to see if that would stop the warning)). But the warning is still happening.
You need to import the MyGizmoClass.h file into your implementation. This lets the compiler know all the MyGizomoClass methods and will prevent the warning.
If this is already the case, then sharedManager isn't properly defined in the MyGizmoClass interface (.h file).