如何修复 Xcode 预期类型错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14401122/
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-15 02:33:10  来源:igfitidea点击:

How to fix Xcode expected type error

iphonexcodecocos2d-iphone

提问by Tom

Today when I opened up Xcode project and built it for the iOS simulator. Everything was working fine yesterday but this morning there is a compilation error.

今天,当我打开 Xcode 项目并为 iOS 模拟器构建它时。昨天一切正常,但今天早上出现编译错误。

The following line is highlighted red in a header file and the message next to it says "Expected a type".

以下行在头文件中以红色突出显示,旁边的消息显示“预期类型”。

-(void) addObstacle:(Obstacle*) obstacle;

The bit of code hasn't changed in a few days so I'm not really sure why theres now an error.

这段代码在几天内没有改变,所以我不确定为什么现在出现错误。

I have imported Obstacle.h and this is the Obstacle class header:

我已经导入 Obstacle.h,这是 Obstacle 类头文件:

#import "kobold2d.h"

@interface Obstacle : CCNode {
    int posXInGrid;
    int posYInGrid;

    CCSprite* sprite;
}

@property (nonatomic) int posXInGrid;
@property (nonatomic) int posYInGrid;
@property (nonatomic, retain) CCSprite* sprite;

@end

回答by LearnCocos2D

Move #import "Obstacle.h"from the interface (.h) to the implementation file (.m).

#import "Obstacle.h"从接口 (.h)移动到实现文件 (.m)。

Then add @class Obstacle; at the top of the interface file.

然后添加@class Obstacle;在接口文件的顶部。

If this fixes the problem you do have a circular import. See hereto learn why this fixes it. @class is preferable over #importing class headers whenever possible.

如果这解决了问题,您就可以进行循环导入。请参阅此处了解为什么会解决此问题。只要有可能,@class 就比 #importing 类头更可取。