Note that there are some explanatory texts on larger screens.

plurals
  1. POInitializing a static singleton object with NSCoder
    primarykey
    data
    text
    <p>I'm working on an iPhone app and facing some troubles with my shared singleton class. I'm using a shared singleton to store two variables <code>int gameRuns</code> and <code>int totalScore</code> 'gamRuns' just increments every time the user loads the app, and 'totalScore' is obvious :D</p> <p>the issue is as follows, I load the singleton and init using my own method when the app loads using this code:</p> <pre><code>+ (SingletonLevelState*)sharedLevelStateInstance { static SingletonLevelState *sharedLevelStateInstance; @synchronized(self) { if(!sharedLevelStateInstance) { //Init a singleton sharedLevelStateInstance = [[SingletonLevelState alloc] init]; sharedLevelStateInstance-&gt;gameRuns = 1; sharedLevelStateInstance-&gt;totalScore = 0; } } return sharedLevelStateInstance; } </code></pre> <p>This is working great as I can reference this class from any other class and always get a pointer to the same object, so this works fine from other objects:</p> <pre><code>sharedLevelState = [SingletonLevelState sharedLevelStateInstance]; sharedLevelStateInstance.gameRuns++; </code></pre> <p>Now I added the NSCoder protocol, and added the two methods <code>initWithCoder</code> and <code>encodeWithCoder</code> as follows :</p> <pre><code>- (void) encodeWithCoder: (NSCoder *)coder { //encode level data [coder encodeInt:self-&gt;gameRuns forKey:@"gameRuns"]; [coder encodeInt:self-&gt;totalScore forKey:@"totalScore"]; } - (id) initWithCoder: (NSCoder *) coder { if(self = [super init]){ self-&gt;gameRuns = [coder decodeIntForKey:@"gameRuns"]; self-&gt;totalScore = [coder decodeIntForKey:@"totalScore"]; } return self; } </code></pre> <p>Now when the app loads, I check to see if we already have a saved sate, if it exists, I just unarchive the class with that file, if not, I init that class using my custom method above, then set its defaults, encode it to file so we have a saved state, here's the code:</p> <pre><code> //Load Level state sharedLevelStateInstance = [SingletonLevelState sharedLevelStateInstance]; //Check if file is saved NSFileManager *fm = [[NSFileManager alloc] init]; NSString *gameStatePath = [NSString stringWithString:[self getSavePath]]; if([fm fileExistsAtPath:gameStatePath]){ [self loadState]; sharedLevelStateInstance.gameRuns = sharedLevelStateInstance.gameRuns+1; NSLog(@"Loaded %d times", [sharedLevelStateInstance gameRuns]); } [fm release]; </code></pre> <p>Now the last line in the if statement works perfectly, it increments every time I load the app as expected and <em>I feel really happy</em> lol.</p> <p><em>However, the problem arises</em> when I try to get a reference of the singleton in another class by doing the following:</p> <pre><code>sharedLevelStateInstance = [SingletonLevelState sharedLevelStateInstance]; NSLog(@"Played: %d times", sharedLevelStateInstance.gameRuns); </code></pre> <p>It always counts back to 1, I know what happens but I'm not sue what's the best way to solve it, when I <code>initWithCoder</code> the singleton, It's not returning a static object, it creates a new one, when I init my <code>sharedLevelStateInstance</code>, it calls my first custom method, initializing it to the defaults hardcoded.</p> <p>So StackOverflow, can you please help me ?!</p> <p>I just need to know what's the best way to get a reference to the same object without allocating a new one every time I <code>initWithCoder</code> !</p> <p>Thanks :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload