jxdwinter
jxdwinter

Reputation: 2369

How to properly import header files in Objective-C

enter image description here

I import the PaiLifeCardLeftViewController.h but Xcode told me it is a unknown type.

How can I fix this, thank you.

edit: PaiLifeCardLeftViewController.h: enter image description here

Upvotes: 0

Views: 91

Answers (2)

rmaddy
rmaddy

Reputation: 318934

This problem is being caused by a circular dependency between PaiLifeCardLeftViewController and PaiLifeCardCenterViewController. Each of the corresponding .h files are trying to import the other. You can't do this.

The proper solution is to update both .h files. In both, remove the import of the other .h and replace it with an @class forward declaration.

PaiLifeCardLeftViewController.h:

#import <UIKit/UIKit.h>
#import "PaiLifeCardRefreshDelegate.h"

@class PaiLifeCardCenterViewController;

@interface PaiLifeCardLeftViewController : UITableViewController

@property (strong, non atomic) id<PaiLifeCardRefreshDelegate> delegate

@end

Make a similar change to PaiLifeCardCenterViewController.h.

Then you must add the import to the .m file.

You should have as few imports in a .h file as possible. It's always better to use forward (@class) declarations when possible. It avoids circular dependencies and it makes compilation a little faster and results in fewer recompiles when .h files are changed.

Side - note. There is no need to declare the instance variable for delegate. It will be synthesized for you.

Upvotes: 3

Richard Brown
Richard Brown

Reputation: 11444

You can make a forward class declaration to the class by adding

@class PaiLifeCardCenterViewController

before the @interface statement.

Upvotes: 1

Related Questions