"Expected a type" error Objective C
circular-reference, objective-c
Solution
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
Problem
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. 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. 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 ``` #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 ``` #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 ``` #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.