Search code examples
iphonexcodesubclassing

Xcode: very strange error subclassing my own class


I have an iPhone app with a custom class, Detail, a subclass of UIViewController that I created.

I need to make a subclass of Detail, and I want to to call it ActivityDetail. So I wrote the following in my ActivityDetail.h file:

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

@interface ActivityDetail : Detail {

}

@end

The problem is that I'm getting a compiler error telling me this:

error: cannot find interface declaration for 'Detail', superclass of 'ActivityDetail'

And the strange thing is: I can change the superclass from Detail to UIView (for example), compile getting many errors (obviously), and then change the superclass to Detail again and everything works fine! But if I then change anything to the Detail class the problem comes back from the beginning...

How can I solve this?


Solution

  • It is recommended to not import classes beyond the default Foundation or UIKit imports in your header files. Instead you should do something similar:

    Header

    #import <UIKit/UIKit.h>
    
    @class Detail;
    
    @interface ActivityDetail : Detail {
    
    }
    
    @end
    

    Implementation

    #import "ActivityDetail.h"
    #import "Detail.h"
    
    @implementation ActivityDetail
    
    @end
    

    This allows your header to "know" about additional classes without forcing all "importers" of that header to also import everything it imports.


    Here is a great reference question, and a great answer, regarding the usage of @class and #import.