I'm creating a game that uses cards. I have an AppController class with one instance in the nib. The AppController instance has an NSArray instance variable called wordList. On init, the nib's instance of AppController generates a new GameCard. Every gamecard has an array of words containing 5 words selected at random from the the list in AppController. Because the list is large, I'd like to read it into memory only once. Therefore, I want only one instance of AppController, as a singleton class. Every time a new GameCard is created from within AppController, it should access that same singleton instance to retrieve the wordlist. So basically, I need a singleton AppController that creates GameCards, where each GameCard has a reference to the original AppController. I'm not sure how to implement this. Sorry if the explanation was confusing.
A code example I found online follows (http://numbergrinder.com/node/29)
+ (AppController *)instance
{
static AppController *instance;
@synchronized(self) {
if(!instance) {
instance = [[AppController alloc] init];
}
}
return instance;
}
But when I tried to do something with it in a GameCard instance through the code below, my application took forever to launch and Xcode told me it was loading 99797 stack frames.
AppController *controller = [AppController instance];