目标C - 错误:'预期类型'

use*_*959 16 import objective-c circular-dependency header-files forward-declaration

我对一些我认为简单的事情有一个非常奇怪的错误.

#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
Run Code Online (Sandbox Code Playgroud)

为什么会问'ViewController'是否是一个类型?这是我正确实施的课程.它也被导入了.

编辑*

如果它有帮助,这是ViewController.m类.

#import "ViewController.h"

@implementation ViewController

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

@end
Run Code Online (Sandbox Code Playgroud)

编辑2 **

和ViewController.h文件

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

@interface ViewController : GLKViewController

@end
Run Code Online (Sandbox Code Playgroud)

jus*_*tin 40

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

如果您使用ViewControllerin GameController,则可以将导入添加到GameController.m.

您可能有循环依赖.

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

  • GameController.h 进口 ViewController.h
  • ViewController.h 进口 GameController.h

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

在真实的代码库中,您可以#import "ViewController.h"在许多源文件中.这会创建非常复杂的依赖项.在ObjC中,这些依赖关系在很大程度上是不必要的 - 您可以在标头中大部分时间使用前向声明(并且它将改善您的构建时间).

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

因此,考虑到OP中的标题,您可以将其重写为:

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
Run Code Online (Sandbox Code Playgroud)

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
...
Run Code Online (Sandbox Code Playgroud)

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