Subclass View Controller: Forward Declaration

I followed the rule:

@MyClassName in class header file

& &

#import "MyClassName" in implementation

Now I decided to subclass one of my controllers. If I rewrite the method, I get the following message:

The receiver of "MyClassName" for the class message is forward declaration

For this to happen, I need to put the #import file in a header file that doesn't seem to match what I think is best.

Can someone explain if I misunderstood the use of @class?

Or if I'm getting it right, can someone explain that you need to break down the best practices and use #import in the header file when subclassing?

Many thanks.

Edit:

Thanks for answers. I think I need to add some details to clarify my situation and hopefully my understanding. Here is my header for my base class:

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"

@class Organisation;

@interface LongCallDetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

      

In my impementation I

#import "Organisation.h"

      

My subclass header contains the following:

#import "LongCallDetailViewController.h"

@interface LongCallSubclassViewController : LongCallDetailViewController

@end

      

If I override a method in a subclass and try to use the Organization object, it gives me an error as I said above. So I need to either add #import "Organization.h" to the base class header, or duplicate #import "Organization.h" in the subclass implementation file. Both of them strike me as wrong.

Many thanks.

+3


source to share


2 answers


Inside the header files, you must import other header files for any classes that you subclass. You don't need to import header files for classes that are only referenced and not subclassed. For example, a header file might look like this:

#import "MySuperClass.h"

@class MyObjectType;

@interface MySubClass : MySuperClass
@property (strong) MyObjectType *value;
@end

      

Edit : Based on your new changes, it looks like you are writing the header files correctly. When you declare @class

in scope only, you will not be able to access any of the selectors or properties associated with that class. It's okay to declare with help @class

in places where you don't intend to use selectors or properties of that class type and just traverse the link (like in the title I described above), but whenever you want to do anything else with the object, you need will import a header file describing @interface

.



When defined @class MyObjectType

in a header file, it is usually expected to #import "MyObjectType.h"

appear in the corresponding source file. Header files are meant to be structure declarations, while source files will contain the implementation. For example, the source file associated with the header file described above might look like this:

#import "MySubClass.h"

#import "MyObjectType.h"

@implementation MySubClass
- (void)overriddenFunction {
  [self.value anObjectTypeSelector];
}
@end

      

You don't have to think about "duplicating" import statements when they are in two different scopes. When you forward your ad @class Organisation

to the header file LongCallDetailViewController

, you will have it #import "Organisation.h"

in the original file LongCallDetailViewController

. If you also need to access these object properties and selectors in your class LongCallSubclassViewController

, you need #import "Organisation.h"

in your implementation file LongCallSubclassViewController

. Remember: implementation files are unaware of each other's content; they only know about the contents of the header file.

+3


source


#import imports a class definition with all public methods and properties.

@import predeclare class so no information about methods and properties



You use #import when:

  • Class subclass - Other classes importing your class need to be aware of all the methods supported by the class.
  • You are implementing the protocol - for the same reason as above.
0


source







All Articles