본문 바로가기

iPhone dev./Objective C general

Cyclic한 import 해결(xcode unknown type name)

stackoverflow.com 참조:

요약하자면 .h에서는 @class로 선언만 해주고 .m에서 실제로 #import하라는 얘기이다.


I got code like this:

Match.h:

#import <Foundation/Foundation.h>
#import "player.h"

@interface Match : NSObject
{
   
Player *firstPlayer;
}

@property (nonatomic, retain) Player *firstPlayer;

@end

Player.h:

#import <Foundation/Foundation.h>
#import "game.h"
@interface Player : NSObject
{
}

- (Player *) init;

//- (NSInteger)numberOfPoints;
//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;


@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *surname;
@property (nonatomic, assign) NSInteger *player_id;
@property (nonatomic, retain) NSString *notes;

@end

Game.h:

#import <Foundation/Foundation.h>
#import "match.h"
#import "player.h"

@interface Game : NSObject
{
   
NSMutableArray *matches;
   
NSMutableArray *players;
   
NSString *name;
}

-(Game *) init;

@property (nonatomic, retain) NSMutableArray *matches;
@property (nonatomic, retain) NSMutableArray *players;
@property (nonatomic, retain) NSString *name;

@end

Xcode won't compile my project and show me error unknown type 'Player' in Match.h when I declare *firstPlayer.

I tried cleaning project, rebuilding it but without any result...

link|edit
4 
You have a cycle in your imports: Match.h imports Player.h imports Game.h imports Match.h. See this question. – Bavarious Oct 26 '11 at 0:07
feedback

The normal way to solve this cycles is to forward declare classes:

In Match.h:

@class Player;
@interface Match ...
   
Player * firstPlayer;

and do #import "Player.h only in Match.m, not in Match.h

Same for the other two .h files.

link|edit
thank you - that helped :) – Esse Oct 26 '11 at 10:19