"预期类型"错误目标C.

Cal*_*YSR 3 objective-c circular-reference

我已经在这里多次询问过这个关于我正在尝试制作的这个红润游戏的问题.我正在研究基于文本的冒险游戏.首先我用Java创建它,因为这就是我在学习游戏所用的类.现在我正在尝试学习需要objective-c的iOS开发.在参加Lynda Essentials课程之后,我对目标c感到非常满意(当然,以前的Java经验帮助过).无论如何我正在研究这个游戏,我遇到的问题似乎与目标c非常不同.

在Java中,当我有多个类时,他们只需要在同一个目录中,以便我在其他类中使用它们.在Objective-C中不是这种情况......如果我想在B类中使用A类,我必须导入头文件.对于这个游戏,我有两个自定义类,一个Location类和一个Exit类.Location类需要知道它有什么退出(所以如果我想使用它我必须导入Exit.h)并且退出需要知道它连接到哪个位置(所以我必须导入Location.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
Run Code Online (Sandbox Code Playgroud)

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

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

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

我真的希望我不想做一些不可能的事情.我也希望我的代码能够理解,而且我不会在这里做太多傻瓜;)感谢任何建议.

Dan*_*imm 5

编辑:重新阅读,现在更好地理解,您需要@class Exit;在Location标头中定义Exit类,然后您可以@class Location;在Exit标头中执行相同操作,以告诉编译器已定义类.然后,如果您要在实现文件(.m)中引用这些类,那么您将分别导入Exit.h文件和Location.h文件