I've asked questions on here so many times about this ruddy game that I'm trying to make. I'm working on a Text-Based adventure game. First I made it in Java because that's what I was learning the the class the game was for. Now I'm trying to learn iOS development which requires objective-c. I feel pretty comfortable with objective c after taking the Lynda Essentials course (The previous experience with Java helped of course). Anyways I'm working on this game and I'm running into a problem that seems pretty unique to objective c.
我在这里已经问了很多次关于这个我想要做的游戏。我正在开发一个基于文本的冒险游戏。首先我用Java做的,因为这就是我学习游戏的目的。现在我正在学习iOS开发需要objective-c。在学习了Lynda Essentials课程后,我对objective c非常满意(当然,之前使用Java的经验对我有帮助)。不管怎样,我在这个游戏中遇到了一个对objective c来说很特别的问题。
In Java when I have multiple classes they just need to be in the same directory in order for me to use them in other classes. This is not the case in Objective-C... I have to import the header files if I want to use class A in class B. Well for this game I have two custom classes, a Location class and an Exit class. The Location class needs to know about what Exits it has (So I have to import Exit.h if I want to use them) and the exits need to know which location it's connected to (So I have to import Location.h). It seems that I can't do this because of something called Circular Referencing (or something like that). However, if I don't do this then I get an "Expected a type" error. So I have no idea what to do. I'll show the code below.
在Java中,当我有多个类时,它们只需要在同一个目录中,以便我在其他类中使用它们。在Objective-C中不是这样的。如果我想在b类中使用A类,我必须导入头文件。对于这个游戏,我有两个自定义类,一个位置类和一个退出类。Location类需要知道它有什么退出(因此我必须导入Exit)。如果我想使用它们,那么出口需要知道它连接到哪个位置(所以我需要导入location。h)。我似乎不能这么做,因为循环引用(或类似的东西)。但是,如果我不这样做,就会得到一个“预期类型”错误。所以我不知道该怎么做。我将显示下面的代码。
Exit.h
Exit.h
#import <Foundation/Foundation.h>
#import "Location.h"
#define NORTH 0
#define SOUTH 1
#define EAST 2
#define WEST 3
@interface Exit : NSObject
@property NSString * dirName;
@property NSString * dirShortName;
@property int direction;
@property Location * connection;
-(id)initWithConnection:(Location *) loc andDirection:(int) dir;
@end
Exit.m
Exit.m
#import "Exit.h"
@implementation Exit
@synthesize dirName;
@synthesize dirShortName;
@synthesize direction;
@synthesize connection;
-(id)initWithConnection:(Location *)loc andDirection:(int)dir {
self = [super init];
if(self) {
direction = dir;
switch(direction) {
case 0:
dirName = @"North";
dirShortName = @"N";
break;
case 1:
dirName = @"South";
dirShortName = @"S";
break;
case 2:
dirName = @"East";
dirShortName = @"E";
break;
case 3:
dirName = @"West";
dirShortName = @"W";
break;
}
connection = loc;
}
return self;
}
@end
Location.h
Location.h
#import <Foundation/Foundation.h>
@interface Location : NSObject
@property NSString * title;
@property NSString * desc;
@property NSMutableDictionary * exits;
@property BOOL final;
-(id) initWithTitle:(NSString *) _title;
-(id) initWithDescription:(NSString *) _desc;
-(id) initWithTitle:(NSString *) _title andDescription:(NSString *) _desc;
-(void) addExit:(Exit *) _exit;
@end
Location.m
Location.m
#import "Location.h"
@implementation Location
@synthesize title;
@synthesize desc;
@synthesize exits;
@synthesize final;
-(void) addExit:(Exit *) _exit {
NSString * tmpName = [_exit dirName];
NSString * tmpShortName = [_exit dirShortName];
[exits setObject:tmpName forKey:tmpShortName];
}
-(NSString *)description {
NSString * tmp = [[NSString alloc] initWithFormat:@"%@\n%@\n",self.title,self.desc];
for(NSString * s in exits) {
[tmp stringByAppendingFormat:@"\n%@",s];
}
return tmp;
}
// Initialization Methods
-(id) init {
self = [super init];
if(self) {
title = @"";
desc = @"";
}
return self;
}
-(id) initWithTitle:(NSString *) _title {
self = [super init];
if(self) {
title = title;
desc = @"";
exits = [[NSMutableDictionary alloc] initWithObjectsAndKeys:nil];
}
return self;
}
-(id) initWithDescription:(NSString *) _desc {
self = [super init];
if(self) {
title = @"";
desc = desc;
exits = [[NSMutableDictionary alloc] initWithObjectsAndKeys:nil];
}
return self;
}
-(id)initWithTitle:(NSString *) _title andDescription:(NSString *)_desc {
self = [super init];
if(self) {
title = title;
desc = desc;
exits = [[NSMutableDictionary alloc] initWithObjectsAndKeys:nil];
}
return self;
}
@end
I'm really hoping I'm not trying to do something that's impossible. I also hope my code can be made sense of and I'm not making too much of a fool of myself here ;) thanks for any advice.
我真的希望我没有尝试做一些不可能的事情。我也希望我的代码能被理解,我不会在这里太出洋相;)谢谢你的任何建议。
2 个解决方案
#1
5
EDIT: Just reread and now understand better, you need to do @class Exit;
to define the Exit class in the Location header and then you can do the same @class Location;
in the Exit header in order to tell the compiler that the classes are defined. Then if you were to reference those classes in the implementation files (.m) then you would import the Exit.h
file and Location.h
file respectively
编辑:重新阅读,现在更好的理解,你需要做@class退出;要在Location头中定义Exit类,然后可以执行相同的@class位置;在退出头中,以告知编译器类已定义。然后,如果您要在实现文件(.m)中引用这些类,那么您将导入出口。h文件和位置。h文件分别
#2
4
The rule of thumb I have started to follow, which seemed counter-intuitive to me at first is this:
我开始遵循的经验法则,一开始对我来说似乎与直觉相反:
In your header files, use "forward declarations" prolifically with only 2 exceptions: headers for classes you are extending, and headers for protocols you are conforming to; and only do #import
directives in your .m
files.
在头文件中,大量使用“前向声明”,只有两个例外:要扩展的类的头文件和要遵循的协议的头文件;并且只在.m文件中执行#导入指令。
This should resolve the circular reference error; it did mine.
这应该可以解决循环引用错误;是我的。
See here, and do a 'find' for the word "forward".
看这里,为“forward”做一个“find”。
#1
5
EDIT: Just reread and now understand better, you need to do @class Exit;
to define the Exit class in the Location header and then you can do the same @class Location;
in the Exit header in order to tell the compiler that the classes are defined. Then if you were to reference those classes in the implementation files (.m) then you would import the Exit.h
file and Location.h
file respectively
编辑:重新阅读,现在更好的理解,你需要做@class退出;要在Location头中定义Exit类,然后可以执行相同的@class位置;在退出头中,以告知编译器类已定义。然后,如果您要在实现文件(.m)中引用这些类,那么您将导入出口。h文件和位置。h文件分别
#2
4
The rule of thumb I have started to follow, which seemed counter-intuitive to me at first is this:
我开始遵循的经验法则,一开始对我来说似乎与直觉相反:
In your header files, use "forward declarations" prolifically with only 2 exceptions: headers for classes you are extending, and headers for protocols you are conforming to; and only do #import
directives in your .m
files.
在头文件中,大量使用“前向声明”,只有两个例外:要扩展的类的头文件和要遵循的协议的头文件;并且只在.m文件中执行#导入指令。
This should resolve the circular reference error; it did mine.
这应该可以解决循环引用错误;是我的。
See here, and do a 'find' for the word "forward".
看这里,为“forward”做一个“find”。