Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

iphone - xcode unknown type name

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...