0

I've been chasing some circular dependencies recently and I'm trying to figure out what the most up to date way of importing things is. Googling isn't much use since that turns up generations upon generations of iterations for this stuff without a clear answer.

What I have now in the MyViewController.h file:

@class ForwardClass;
@protocol ForwardProtocol;
typedef NS_ENUM(NSUInteger, XYZCharacterType);

@interface MyViewController: UIViewController

@property (strong, nonatomic) ForwardClass *fw;

@property (nonatomic, strong) id<ForwardProtocol> fwProtocol;

@property XYZCharacterType charType;

@end

and in the MyViewController.m file:

@import MBProgressHUD;

#import "SomeManager.h"
#import "SomeOtherViewController.h"
#import "Model.h"

#import "MyViewController.h"

#import "MyProject-Swift.h"

@implementation MyViewController

@end

So the .h file does not import anything anymore and only forward declares the files that it needs to get a handle on.

Is this the most current way of doing things? Are there any limitations to this that I should be aware of?

Alper
  • 3,424
  • 4
  • 39
  • 45

1 Answers1

1

Your .h is the best and most modern way of doing it, you should always avoid any #import in .h files.

From my experience most people still prefer to use #import <Cocoa/Cocoa.h> over @import Cocoa;.

If you opt-in to use modules #import will do @import automatically so there is no need to use it.

Also its clearer which dependencies are being used in this class, and it won't import anything that you might not need in the current scope.

Syloslerca
  • 56
  • 4