“Expected method body” error

后端 未结 8 1350
天涯浪人
天涯浪人 2020-12-20 13:15

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

Expected

相关标签:
8条回答
  • 2020-12-20 13:41

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

    0 讨论(0)
  • 2020-12-20 13:44

    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.

    0 讨论(0)
  • 2020-12-20 13:48

    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.

    0 讨论(0)
  • 2020-12-20 13:51

    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.

    0 讨论(0)
  • 2020-12-20 13:56

    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.

    0 讨论(0)
  • 2020-12-20 14:00

    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.

    0 讨论(0)
提交回复
热议问题