Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS - encoding/decoding enums - crash on accessing after relaunch
    text
    copied!<p>What am I doing wrong? Am I encoding/decoding the enum type properly?</p> <p>GameSettings interface:</p> <pre><code>typedef enum { CatWhite = 0, CatBlack = 1, CatOrange = 2 } CatColor; ... CatColor catColor; ... @property CatColor catColor; </code></pre> <p>GameSettings implementation:</p> <pre><code>@synthesize catColor; ... + (GameSettings*)GetInstance { if (sharedSingleton == nil) { sharedSingleton = [[super allocWithZone:NULL] init]; sharedSingleton.catColor = CatWhite; } return sharedSingleton; } -(void)encodeWithCoder:(NSCoder *)coder { [coder encodeInt:self.catColor forKey:@"CatColor"]; } -(id)initWithCoder:(NSCoder *)coder { if((self = [super init])) { self.catColor = [coder decodeIntForKey:@"CatColor"]; } NSLog(@"initWithCoder: %d", self.catColor); //this logs the correct int return self; } AppDidFinishLaunching: - (void)applicationDidFinishLaunching:(UIApplication *)application { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSData *data = [defaults objectForKey:@"GameSettings"]; GameSettings *gGameData = [NSKeyedUnarchiver unarchiveObjectWithData:data]; if(gGameData != NULL) { GameSettings *settings = [GameSettings GetInstance]; settings.catColor = gGameData.catColor; }else { GameSettings *settings = [GameSettings GetInstance]; settings.catColor = CatWhite; } ... } </code></pre> <p>Save settings:</p> <pre><code>GameSettings* settings = [GameSettings GetInstance]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSData *data = [NSKeyedArchiver archivedDataWithRootObject:settings]; [defaults setObject:data forKey:@"GameSettings"]; [defaults synchronize]; </code></pre> <p>The crash (Program received signal: “EXC_BAD_ACCESS”) comes when I re-launch the app and try to access the game settings:</p> <pre><code>GameSettings* settings = [GameSettings GetInstance]; NSLog(@"settings: %d", settings); //EXC_BAD_ACCESS NSLog(@"catColor: %d", settings.catColor); //EXC_BAD_ACCESS </code></pre> <p>Why can't I access the GameSettings singleton after a re-launch? </p>
 

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