Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

“Expected method body” error

I had my application working fine, then without doing anything, out of nowhere I got 2 errors in appDelegate.h. One says this:

Expected selector for Objective-C method

The other says this:

Expected method body

I have no idea why this is happening, I have other projects with the exact same app delegate and they all work just fine.

This is my appDelegate.h:

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate> // I get the errors here

@property (strong, nonatomic) UITabBarController *tbc; 
@property(strong, nonatomic) UIWindow *window;

@end
like image 803
Rafael Moreira Avatar asked Jan 25 '12 01:01

Rafael Moreira


3 Answers

I had the same problem. At last I found that in my main.m I had accidentally added a "-" character in the beginning of the file.

Removing the character solved the problem.

like image 110
Cnxiaowei Avatar answered Nov 04 '22 22:11

Cnxiaowei


I usually find that a mysterious error like this happens because I accidentally typed a stray character into one of my other source files - either at the end of one of the other header files, or at the top of a .m file.

Look at the top of the .m file that Xcode is trying to compile. Check it for stray characters. If you don't find any, look at which file is imported just before AppDelegate.h. Check for stray characters at the end of that other header file. If you have header files that import AppDelegate.h, you may have to check those too. (There's really no reason any other .h file should have to import AppDelegate.h.)

like image 30
rob mayoff Avatar answered Nov 04 '22 23:11

rob mayoff


Try closing Xcode and then reopening and doing a clean build.

If that doesn't fix it, it's possible you've got a circular reference in one of your header files.

This can happen when foo.h #imports "bar.h" and bar.h #imports "foo.h" (or sometimes its a chain of three or more header files importing each other in a circle) and it leads to spurious errors like the one you're seeing.

The solution is to try to avoid importing headers in your .h files, and instead use @class references for external classes in the .h files and put the #imports in the .m files instead.

like image 3
Nick Lockwood Avatar answered Nov 04 '22 21:11

Nick Lockwood