Objective-C 头文件无法将自定义对象识别为类型 [英] Objective-C header file not recognizing custom object as a type
问题描述
我正在使用 cocos2d 为 iPad 开发一个游戏,该游戏涉及一个充满不同类型图块的板.我创建了一个名为 Tile
的自定义类作为磁贴的通用模板,以及一些具有不同属性和方法的 Tile
子类.我还创建了一个名为 Board
的类,除其他外,它使用特殊坐标系跟踪所有图块的位置.
I'm working on a game for iPad using cocos2d which involves a board filled with different types of tiles. I've created a custom class called Tile
as a general template for tiles and a few subclasses of Tile
which have different properties and methods. I've also created a class called Board
which, among other things, keeps track of the locations of all the tiles using a special coordinate system.
出于某种原因,在 Board
类中,编译器似乎没有将 Tile
识别为一种对象,即使我已经添加了 #import "Tile.h"
在文件的顶部.
For some reason, in the Board
class, the compiler doesn't seem to be recognizing Tile
as a type of object, even though I've added #import "Tile.h"
at the top of the file.
这是相关的代码(直接问有没有你想看的其他部分的代码):
Here's the relevant code (just ask if there's other parts of the code you want to see):
Tile.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Board.h"
@interface Tile : NSObject
-(void) updateNeighbors;
@property (nonatomic, retain) CCSprite* sprite;
@property (assign) CGPoint coords;
@property (assign) CGPoint positionInPoints;
@property (nonatomic, retain) NSMutableArray *neighbors;
@end
Board.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Tile.h"
@interface Board : NSObject
+(Board*)sharedBoard;
- (void) putTile: (Tile*) tile AtIndex: (CGPoint) index; //<-- error here!
- (void) replaceTileAtIndex: (CGPoint) index1 WithTileAtIndex: (CGPoint) index2;
- (Tile*) tileAtIndex: (CGPoint) index; //<-- error here!
- (void) populate;
@property (nonatomic, retain) NSMutableArray *tiles;
@property (nonatomic, retain) NSString *type;
@property (assign) CGPoint size;
@end
此代码甚至无法构建,并且在指示的地方出现以下错误:
This code will not even build and I'm getting the following error where indicated:
'Tile' 前应为 '('
Expected '(' before 'Tile'
如果我将类型从 (Tile*)
更改为 (NSObject*)
,它会修复错误,这让我相信 Tile代码>未被识别为对象类型.
If I change the type from (Tile*)
to (NSObject*)
, it fixes the error, which leads me to believe that Tile
is not being recognized as a type of object.
我通过 Google 和本网站进行了搜索,但无法弄清楚为什么会发生这种情况.
I've searched via Google and this site and cannot figure out why this is happening.
愚蠢的错误;易于修复.
Dumb mistake; easy to fix.
正如大家所指出的,问题是两个头文件相互导入,这是不允许的.现在,我通过将#import "Board.h" 语句移到 Tile.m 来解决这个问题,因为头文件中不需要它.稍后,如果我决定在 Tile.h 文件中使用 Board,我将使用前向引用 (@class Board;),正如你们中的一些人所建议的那样.
As you all have pointed out the problem is that the two header files are importing each other, which is not allowed. For now, I've fixed the problem by moving the #import "Board.h" statement to Tile.m, since it isn't needed in the header file. Later on, if I decide to use Board in the Tile.h file I will use forward referencing (@class Board;), as a few of you suggested.
再次感谢!
推荐答案
这是标题导入标题的经典问题.你在这里有一个圆圈:Tile.h 正在导入 Board.h,它导入了 Tile.h.这让编译器感到困惑——它陷入了一个循环.
This is a classic problem with headers importing headers. You have a circle here: Tile.h is importing Board.h, which imports Tile.h. This confuses the compiler -- it gets stuck in a loop.
您可以通过不将标头导入标头来解决此问题.但是,您仍然需要让编译器知道 Tile
.在 Board.h 中,创建一个 "前向声明"类:
You solve this by not importing headers into headers. You still need to let the compiler know about Tile
, however. In Board.h, make a "forward declaration" of the class:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@class Tile; // Dear compiler,
// Tile is a class that I will need to refer
// to in this file. Please consider it to be a
// type; I promise it'll be defined at runtime.
// Sincerely, stephenalexbrowne
@interface Board : NSObject
//etc.
这向编译器保证有一个名为Tile
的类将在运行时存在;然后,您可以在标题的其余部分引用该名称.在您的 Board
实现中,您导入 Tile.h.这将使编译器在需要它们的地方看到与 Tile
类关联的方法和属性.
This assures the compiler that there is a class called Tile
that will exist at runtime; you can then refer to that name in the remainder of your header. In your implementation for Board
, you import Tile.h. That will let the compiler see the methods and properties associated with the Tile
class where they are needed.
同样,将 #import "Board.h"
移动到 Tile.m.由于您不是在 Tile.h 中引用 Board
类,因此您无需进行前向声明.
Likewise, move the #import "Board.h"
into Tile.m. Since you aren't referring to the Board
class in Tile.h, you don't need to make a forward declaration.
一般来说,最好只将您的类头文件导入到需要它们的实现文件中.框架头文件,因为它们永远不会导致您的代码循环,因此可以并且——因为您需要引用其中声明的许多类——应该导入到您的头文件中.
In general, it is best to import your class headers only into the implementation files where they are needed. Framework headers, since they will never cause a cycle with your code, can and -- because you need to refer to many of the classes declared in them -- should be imported into your headers.
这篇关于Objective-C 头文件无法将自定义对象识别为类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!