问题描述
我正在使用cocos2d的iPad游戏,其中包含一个填充了不同类型的瓷砖的板。我创建了一个自定义类 Tile
作为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
作为对象类型,即使我已经在文件的顶部添加了 #importTile.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 / p>
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 *)
更改为(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.
正如你们都指出的问题是,两个头文件正在导入每个其他,这是不允许的现在,我已经解决了这个问题,通过移动#importBoard.h语句到Tile.m,因为它不是在头文件中需要的,后来,如果我决定使用Board.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.
同样,将 #importBoard.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头文件不能将自定义对象识别为类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!