Question

I have a big project which is refactored a lot.

Some classes import unnecessary headers.

#import "someClass1.h"

#import "someClass2.h"

#import "someClass3.h"

f.e:

classA in interface has:

#import "classB.h"

#import "classC.h"

classB in interface has:

#import "classC.h"

So that import classC in classA interface could be removed...

I would like to have my project clean and tidy.

I'm just wondering:

  • Is there a way to find useless imports quickly?
  • What is best way for such situations and organizing one interface import of other interfaces?
  • Does it affect on application performance if you have plenty of useless/duplicated imports?

Any help appreciated

Was it helpful?

Solution

1) No there is no a way you need to check if that import is used in your file (use find function for this);

2) The best way is import UNTIL IS POSSIBLE header in implementation file (.m). If you need to declare that class in your header file (.h) you can use:

@class nameClass

before begin with @interface...etc. If you already know that instead you need to import that class header file in your header, you can do it. Be careful to avoid #import cycle. In fact suppose to have this situation:

MGCustomViewController.h:

#import "MGViewController.h"

@interface MGCustomViewController : UIViewController

@end

MGViewController.h:

#import "MGCustomViewController.h"

@interface MGViewController : MGCustomViewController

@end

This cause an error at compile time, because you inherit from MGCustomViewController but importing MGCustomViewController.h you are importing again this last header before that the compiler read the @interface MGCustomViewController directive and so will show an error that says something like SUPERCLASS NOT FOUND.

3) No, but effects on the compile time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top