Skip to content

Instantly share code, notes, and snippets.

@quantumpotato
Created September 12, 2012 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quantumpotato/3709551 to your computer and use it in GitHub Desktop.
Save quantumpotato/3709551 to your computer and use it in GitHub Desktop.
Avoiding multiple #import .h when using @Class forward declaration
//QPBattlefield.h
#import "ClonePilotBattlefield.h"
#import "QPBFState.h"
#import "QPBFTitleState.h"
#import "QPBFDrawingState.h"
#import "QPBFInputConstants.h"
@interface QPBattlefield : ClonePilotBattlefield
@property (nonatomic, retain) QPBFState *currentState;
@property (nonatomic, retain) QPBFTitleState *titleState;
@property (nonatomic, retain) QPBFDrawingState *drawingState;
- (void)changeState:(QPBFState *)state;
@end
//QPBFState.h
#import <Foundation/Foundation.h>
@class QPBattlefield;
@interface QPBFState : NSObject
@property (nonatomic, retain) QPBattlefield *f;
- (void)addTouch:(CGPoint)l;
- (id)initWithBattlefield:(QPBattlefield *)field;
@end
//QPBFTitleState.m ((inherits from QPBFState))
#import "QPBFTitleState.h"
#import "QPBattlefield.h"
@implementation QPBFTitleState
- (void)addTouch:(CGPoint)l {
if (GetDistance(l, self.f.player.l) <= QPBF_PLAYER_TAP_RANGE) {
[self.f changeState:self.f.drawingState];
}
}
@end
@quantumpotato
Copy link
Author

Line 4 & 39. How can I avoid doing this in every subclass of QPBFState or, why doesn't @Class work as I want it to?

I could use a @protocol. I want to know if this is possible.

@quantumpotato
Copy link
Author

I want the compiler to know about QPBattlefield's properties from a @Class. Compiler flag somewhere.. ?

@subdigital
Copy link

@class doesn't promise any interface. It just promises that it will exist at compile time. Why can't you just do this again?

#import "QPBattleField.h"

@iovortex
Copy link

All @Class does is tell a class that "a class with this name does/will exist". There's no information about what the class contains until you import the header.

@quantumpotato
Copy link
Author

Suspicions confirmed. Thanks everyone.

Circular #imports would work if only ..

http://www.programmers-pain.de/wp-content/uploads/recursion.jpg

@quantumpotato
Copy link
Author

@subdigital Definitely can. Just seeing if I could avoid typing that one extra line N*subclasses times in this design.

@subdigital
Copy link

You can put it in your PCH file maybe.

@subdigital
Copy link

recursion

@quantumpotato
Copy link
Author

@subdigital I'm about to get a haircut. Inspired.

@joshrieken
Copy link

omgwtfbbq haircut?!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment