开发者

Fix an error in an objective c tutorial

开发者 https://www.devze.com 2023-02-10 16:40 出处:网络
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. Th

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 err开发者_如何学Pythonor 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 ;-)

0

精彩评论

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