Question

I have read question after question about people getting the same error as me, but I simply do not understand them, so before you go searching for duplicate questions, maybe someone can explain to me what I am doing wrong with this subclassing deal.

I have a subclass of UIImageView called swapView that I want to subclass to override the method -(void)count for special cases. I went to subclass this as I have any pre-existing UIKit class, but when I tried to build and run the project, I get this error:

Attempting to use the forward class 'swapView' as superclass of 'coinView'

I have tried putting both the #import statement of swapView and @class swapView in coinView.h and I've tried putting the import statement in coinView.m, but it refuses to build because of this continued error. If I move the import statement into the .m file, all references to the superclass's methods and properties, such as @property (nonatomic) int max; cause errors as well.

What am I doing wrong?

swapView.h

#import <UIKit/UIKit.h>

#import "ViewController.h"

@class ViewController;

@interface swapView : UIImageView
{
    NSTimer* tmr;
}

@property (nonatomic) int current;
@property (nonatomic) int max;
@property (nonatomic, retain) UIImage* firstImage;
@property (nonatomic, retain) UIImage* secondImage;
@property (nonatomic) BOOL smallMax;

@property (nonatomic, retain) ViewController* pvc;

- (BOOL)testCollision:(CGPoint)point;
- (float)randomFloatBetween:(float)smallNumber bigNumber:(float)bigNumber;

@end

coinView.h

#import "swapView.h"

@class swapView;

@interface coinView : swapView

- (void)count;
- (void)move;

@end
Was it helpful?

Solution

For inheritance, the superclass MUST be inherited.

coinView.h

#import "swapView.h"
@interface coinView : swapView
  - (void)count;
  - (void)move;
@end

You're both forward declaring and importing ViewController.h in your swapView, which may cause compiler to complain.

swapView.h

#import <UIKit/UIKit.h>

@class ViewController

@interface swapView : UIImageView
.
.
.
@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top