xcode “预期的方法体”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8996811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:05:35  来源:igfitidea点击:

“Expected method body” error

objective-cxcode

提问by Rafael Moreira

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

我的应用程序运行良好,然后什么也没做,我突然在appDelegate.h. 一个是这样说的:

Expected selector for Objective-C method

Objective-C 方法的预期选择器

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:

这是我的 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

回答by Cnxiaowei

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.

我有同样的问题。最后我发现在我的 main.m 中,我不小心在文件开头添加了一个“-”字符。

Removing the character solved the problem.

删除字符解决了问题。

回答by rob mayoff

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

我通常会发现像这样的神秘错误发生是因为我不小心在我的其他源文件之一中键入了一个杂散字符 - 要么在其他头文件的末尾,要么在文件的顶部.m

Look at the top of the .mfile 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 .hfile should have to import AppDelegate.h.)

查看.mXcode 试图编译的文件的顶部。检查它是否有杂散字符。如果没有找到,请查看在AppDelegate.h. 检查其他头文件末尾的杂散字符。如果您有 import 的头文件AppDelegate.h,您可能也必须检查它们。(真的没有理由.h必须导入任何其他文件AppDelegate.h。)

回答by Nick Lockwood

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

尝试关闭 Xcode,然后重新打开并进行干净的构建。

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.

这可能发生在 foo.h #imports "bar.h" 和 bar.h #imports "foo.h"(或者有时它是一个由三个或更多头文件组成的链,在一个圆圈中相互导入)并导致虚假错误就像你看到的那个。

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.

解决方案是尽量避免在 .h 文件中导入标头,而是对 .h 文件中的外部类使用 @class 引用,而将 #imports 放在 .m 文件中。

回答by Jeyhun Karimov

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

因为我是目标 C 的新手,所以我忘记在发送函数参数时“不使用”左括号和右括号

回答by Honey

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.

对我来说,这是因为 Apple 预定义方法的名称拼写错误。

回答by philippe

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

从 Internet 粘贴一些代码后,也会出现此消息。您的代码段中可能有一些不可见的字符。

  • 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).
  • 你在 XCode 中看到的不可见字符关闭
    你在 XCode 中看到的
  • 隐藏的内容(在另一个文本编辑器中查看)
    隐藏的内容(在另一个文本编辑器中查看)
  • XCode 7.3 显示的不可见字符打开*
    XCode 7.3 打开不可见字符后显示的内容
    您可以通过进入“首选项...>键绑定”配置 XCode 7.3 以显示不可见字符。找到“隐形”并选择一个组合键(例如 Command+shift+F1)。

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.

因此,如果您遇到“预期方法主体”错误,其中一种解决方案(请参阅上面的其他方法...)是从头开始重新键入错误的行。

回答by knarf

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.

就我而言,我将实现文件中的新方法复制到头文件中。我还复制了@implementation Class(category) 行并忘记将其更改为@interface。

回答by Phillip Kamikaze

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

就我而言,变量名保留给 C++,因为我将文件更改为 *.mm