I have two objects, DNLocation
s, that have arrays of other locations inside of them. The idea i'm trying to get across is that you can go from one location to another, and back to the previous one. Unfortunately, when i create the locations like this, they keep creating each other resulting in an infinite loop. I'm not sure what to do here. I've seen circular references but this seems like something else.
+ (DNLocation *) aCell {
static dispatch_once_t onceToken;
static DNLocation *instance = nil;
dispatch_once(&onceToken, ^{
instance = [DNLocation locationWithName:@"A Cell" andActions:[NSArray arrayWithObjects: [DNAction doNothingAction], nil] andLocations:[NSArray arrayWithObjects:[DNLocation aWhiteRoom], nil]];
});
return instance;
}
+ (DNLocation *) aWhiteRoom {
static dispatch_once_t onceToken;
static DNLocation *instance = nil;
dispatch_once(&onceToken, ^{
instance = [DNLocation locationWithName:@"A White Room" andActions:nil andLocations:[NSArray arrayWithObjects:[DNLocation aCell], nil]];
});
[instance setColor:[UIColor DNWhiteColor]];
[instance setFontColor:[UIColor DNBlackColor]];
return instance;
}
+ (DNLocation *) locationWithName:(NSString *) name {
return [[DNLocation alloc] initWithName:name];
}
These methods are called upon load of the app, setting up all of the possible locations to go to.
I'd rather not have to instantiate them and then add the locations to themselves, but if that is the only way then i'll do it.