目标C - 错误:'预期类型' [英] Objective C - Error: 'Expected a type'

查看:179
本文介绍了目标C - 错误:'预期类型'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一些我认为很简单的事情上得到了一个非常奇怪的错误。

I'm getting a very strange error on something that I would have thought to be simple.

#import <Foundation/Foundation.h>
#import "ViewController.h"
#import "GameObject.h"


@interface GameController : NSObject 

@property (strong) GLKBaseEffect * effect;
@property (strong) NSMutableArray * gameObjects;
@property (strong) NSMutableArray * objectsToRemove;
@property (strong) NSMutableArray * objectsToAdd;


+ (GameController *) sharedGameController;
- (void) tick:(float)dt;
- (void) initializeGame: (ViewController*) viewcontroller;//ERROR: EXPECTED A TYPE

- (void) createObject:(Class) objecttype atPoint:(CGPoint)position;
- (void) deleteObject:(GameObject*) object atPoint:(CGPoint)position;
- (void) manageObjects;

@end

为什么会质疑'ViewController'是否是一种?这是我正确实施的课程。它也已导入。

Why would it question whether or not 'ViewController' is a type? It's a class that I've correctly implemented. It has also been imported.

编辑 *

这是ViewController。 m class如果它有帮助。

Here is the ViewController.m class if it helps.

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[GameController sharedGameController] initializeGame:self];
}

@end

编辑2 * *

和ViewController.h文件

and the ViewController.h file

#import <GLKit/GLKit.h>
#import "GameController.h" 

@interface ViewController : GLKViewController

@end


推荐答案

使用前向声明: @class ViewController; 代替 #importViewController.h。在Objective-C中的另一个标头中通常不需要导入。

Use a forward declaration: @class ViewController; in place of #import "ViewController.h". The import is usually unnecessary in another header in Objective-C.

如果在<$ c $中使用 ViewController c> GameController ,然后你可以将导入添加到GameController.m。

If you use ViewController in GameController, then you can add the import to GameController.m.

你可能有循环依赖。

定义循环依赖的基本方法是:

The basic way to define a circular dependency is:


  • GameController.h imports ViewController.h

  • ViewController。 h 进口 GameController.h

  • GameController.h imports ViewController.h
  • ViewController.h imports GameController.h

哪一个首先会看到翻译中的声明顺序,但显然必须首先考虑,因为在这种情况下,标题不一致必须先出现。

Which one will be seen first depends on the order of declaration in the translation, but obviously one will have to come first because in this case the headers disagree as to which must come first.

在实际代码库中,您可以在许多源文件中 #importViewController.h。这会创建非常复杂的依赖项。在ObjC中,这些依赖项基本上是不必要的 - 你可以在标题中大部分时间使用前向声明(它会改善你的构建时间)。

In a real codebase, you may #import "ViewController.h" in many source files. This creates very complex dependencies. These dependencies are largely unnecessary in ObjC -- you can use forward declarations most of the time in a header (and it will improve your build times).

所以我解释了最简单的情况,但当15个标题 #import ViewController.h 时会发生什么?那么,你必须追踪哪个标题引入了该翻译的循环依赖。如果您不能很好地管理依赖项,那么您可能需要逐步执行数十(或数百)个文件。有时,最简单的方法是查看该翻译的预处理输出(例如,违规的 * .m 文件)。如果依赖关系没有最小化,那么输出可能是数十万行(如果管理正确,您的构建时间可能会快20倍或更多)。因此,在大型代码库中快速定位循环依赖的复杂性;您在标题中 #import #include 越多。在标题中使用前向声明(如果可能)解决了这个问题。

So I explained the simplest case, but what happens when 15 headers #import ViewController.h? Well, you would have to track down which header is introducing that circular dependency for that translation. If you don't manage dependencies well, then you may have to step through dozens (or hundreds) of files. Sometimes, it's easiest to just review the preprocessed output for that translation (e.g. the offending *.m file). If dependencies are not minimized, that output could be hundreds of thousands of lines (and your build times could be 20 or more times faster if it were managed correctly). So the complexity of locating circular dependencies quickly goes up in large codebases; the more you #import and #include in the headers. Using forward declarations in headers (where possible) solves this problem.

示例

因此,如果在OP中标题,您可以将其重写为:

So given your header in the OP, you could rewrite it as:

GameController.h

GameController.h

// includes
#import <Foundation/Foundation.h>

// forwards required by this header    
@class GameObject;
@class GLKBaseEffect;
@class ViewController;

// header declarations
@interface GameController : NSObject 

@property (strong) GLKBaseEffect * effect;
@property (strong) NSMutableArray * gameObjects;
@property (strong) NSMutableArray * objectsToRemove;
@property (strong) NSMutableArray * objectsToAdd;


+ (GameController *) sharedGameController;
- (void) tick:(float)dt;
- (void) initializeGame: (ViewController*) viewcontroller;//ERROR: EXPECTED A TYPE

- (void) createObject:(Class) objecttype atPoint:(CGPoint)position;
- (void) deleteObject:(GameObject*) object atPoint:(CGPoint)position;
- (void) manageObjects;

@end

GameController.m

GameController.m

#import "GameController.h"
#import "ViewController.h" // << if you need it in this source
#import "GameObject.h" // << if you need it in this source

@implementation GameController
...

然后你可以对ViewController.h(导入GameController.h)应用相同的处理。

Then you can apply the same treatment to ViewController.h (which is importing GameController.h).

这篇关于目标C - 错误:'预期类型'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆