I’ve got the following code:
#import <Foundation/Foundation.h>
#import "Game.h"
@interface World : NSObject
@property (nonatomic, strong) Game *game;
+(id)sharedInstance;
//---------------------------------------
#import "World.h"
@implementation World
@synthesize game = _game;
+(id)sharedInstance {
DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
return [[self alloc] init];
});
}
Yet when I try to set the game property:
-(id)initWithLevelIdentifier:(int)identifier {
if (self = [super init]) {
self.currentLevel = [[Level alloc] initWithIdentifier:identifier];
// stuff
[[World sharedInstance] setGame:self];
}
return self;
}
I get:
“Cannot initialize a parameter of type ‘int *’ with an lvalue of type ‘Game *__strong'”
Why does it think this is an int *, when it’s clearly specified as a Game type?
You have a circular dependency here. I’m betting
Game.halso importsWorld.h. See @class vs. #import in header compile time saving with Clang?.The solution is to just note in
World.hthatGameis a class, but not import the header:Note that your code here has a design problem as well. The mere act of creating a
Gameobject changes the currentWorldgame. This means that it is very difficult to treatGameas a non-singleton (just creating it modifies global state), yetGameis not a singleton. This is very surprising behavior in an initialization method. It would be better to move thesetGame:call outside ofinitand let the caller decide whether this is now the global current game. It would be reasonable to put this inWorldas-[World createNewGame].