objective-c – Objective C – 错误:’预期一个类型’
我会觉得很简单的东西,我会发现一个非常奇怪的错误.
#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”是否是一种类型?这是我正确实施的课程.它也被导入. 编辑* 这是ViewController.m类,如果它有帮助. #import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [[GameController sharedGameController] initializeGame:self]; } @end 编辑2 ** 和ViewController.h文件 #import <GLKit/GLKit.h> #import "GameController.h" @interface ViewController : GLKViewController @end 解决方法
使用前向声明:@class ViewController;代替#import“ViewController.h”.在Objective-C的另一个标题中通常不需要导入.
如果您在GameController中使用ViewController,则可以将导入添加到GameController.m. 你可能有循环依赖. 定义循环依赖的基本方法是: > GameController.h导入ViewController.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 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)应用相同的处理方式. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |