0

I am quite new on Objective C so please be a bit slow with me.

I have constructed my Views like this: ViewController=>Root(View)=>Controls(View). Root is a Singleton so I can get the root element of my App any time.

When I add #import "Root.h" to Controls.h I get a parse issue.

#import <UIKit/UIKit.h>
#import "Root.h"      // triggers the error

@interface Controls : UIView

@end

Here my Root.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Controls.h"
#import "Content.h"

@interface Root : UIView
{
    Controls *m_controls; // Parse error: Unknown type name "Controls"
}
+(Root*)getInstance;
@end

What causes that? How to fix?

Delimitry
  • 2,987
  • 4
  • 30
  • 39
asdf
  • 1,475
  • 1
  • 9
  • 11

2 Answers2

2

You could use #import *.h only in *.m file, when you need use some class in *.h you could use @class.

So, you must change your class like this:

#import <UIKit/UIKit.h>
@class Root 
@interface Controls : UIView
@end

and in *.m file:

#import "Root.h"

Roman Barzyczak
  • 3,785
  • 1
  • 30
  • 44
1

You have an import dependency loop. Root.h imports Controls.h and vice versa. One of them has to go first, and that one doesn't know about the declaration in the other.

In general, do as many of your imports in the implementation (.m) file as possible and use forward declarations in the .h like this:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Content.h"

@class Controls;

@interface Root : UIView
{
    Controls *m_controls;  //Prase error: Unknown type name "Controls"
}
+(Root*)getInstance;
@end

Incidentally, you can now put instance variables in the @implementation insead of the @interface

JeremyP
  • 84,577
  • 15
  • 123
  • 161