Rafael Moreira
Rafael Moreira

Reputation: 1769

“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

Upvotes: 12

Views: 21776

Answers (10)

Raphael Pinel
Raphael Pinel

Reputation: 2790

I had the same error because I was using RCT_EXPORT_METHOD instead of RCT_EXTERN_METHOD

Upvotes: 3

SmallChess
SmallChess

Reputation: 8116

This message could come up with some invisible characters. Paste your code in another text editor such as TextMate you will see it.

enter image description here

Upvotes: 0

knarf
knarf

Reputation: 732

In my case I copied over my new methods in my implementation file over to the header file. I also copied over the @implementation Class(category) line and forgot to change it to @interface.

Upvotes: 0

philippe
philippe

Reputation: 1957

This message can also happen after pasting some code from Internet. There may be some invisible characters in your snippet.

  • What you see in XCode with invisible chars OFF
    What you see in XCode
  • What's hidden (viewed in another text editor)
    What's hidden (viewed in another text editor)
  • What XCode 7.3 shows with invisible chars turned ON*
    What XCode 7.3 shows with invisible chars turned ON
    You can configure XCode 7.3 to show the invisible characters by going in "Preferences...>Key bindings". Find "invisible" and choose a key combination (Command+shift+F1 for instance).

So ONE of the solutions (see others above...) if you have the "Expected Method Body" error is to re-type the erroneous line from scratch.

Upvotes: 0

mfaani
mfaani

Reputation: 36287

Basically the

Expected method body

is because there is a type/extra character somewhere!

For me it was because the name of Apple's pre-defined's method was mis-spelled.

Upvotes: 0

Jeyhun Karimov
Jeyhun Karimov

Reputation: 1295

I had the same problem it was like:

-(void) gotoHome(){ ...}

Because I am new to objective C, I forgot to "not using" opening and closing braces when send function arguments

Upvotes: 3

Phillip Kamikaze
Phillip Kamikaze

Reputation: 144

In my case, variable name was reserved to C++ because i change my files to *.mm

Upvotes: 0

Cnxiaowei
Cnxiaowei

Reputation: 231

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.

Upvotes: 23

rob mayoff
rob mayoff

Reputation: 385590

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.)

Upvotes: 13

Nick Lockwood
Nick Lockwood

Reputation: 40995

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.

Upvotes: 3

Related Questions