简体   繁体   English

iOS-编码/解码枚举-重新启动后在访问时崩溃

[英]iOS - encoding/decoding enums - crash on accessing after relaunch

What am I doing wrong? 我究竟做错了什么? Am I encoding/decoding the enum type properly? 我是否可以正确编码/解码枚举类型?

GameSettings interface: GameSettings界面:

typedef enum {
    CatWhite = 0,
    CatBlack = 1,
    CatOrange = 2
} CatColor;

...
CatColor catColor;
...
@property CatColor catColor;

GameSettings implementation: GameSettings实现:

@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;
        }
        ...
    }

Save settings: 保存设置:

GameSettings* settings = [GameSettings GetInstance];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:settings];
[defaults setObject:data forKey:@"GameSettings"];
[defaults synchronize];

The crash (Program received signal: “EXC_BAD_ACCESS”) comes when I re-launch the app and try to access the game settings: 当我重新启动应用程序并尝试访问游戏设置时,发生崩溃(程序收到信号:“ EXC_BAD_ACCESS”):

GameSettings* settings = [GameSettings GetInstance];
NSLog(@"settings: %d", settings); //EXC_BAD_ACCESS
NSLog(@"catColor: %d", settings.catColor); //EXC_BAD_ACCESS

Why can't I access the GameSettings singleton after a re-launch? 重新启动后为什么不能访问GameSettings单例?

Are you sure you want to use applicationDidFinishLaunching there? 您确定要在此处使用applicationDidFinishLaunching吗? When you resurrect the program, it may be invoking awakeFromNib instead of applicationDidFinishLaunching (source: http://www.cimgf.com/2008/03/26/cocoa-tutorial-awakefromnib-vs-applicationdidfinishlaunching/ ). 当您重新启动该程序时,它可能正在调用awakeFromNib而不是applicationDidFinishLaunching(来源: http : //www.cimgf.com/2008/03/26/cocoa-tutorial-awakefromnib-vs-applicationdidfinishlaunching/ )。

Finally figured this out. 终于明白了。 GameSettings was getting deallocated because NSKeyedUnarchiver was autoreleasing it: GameSettings被释放,因为NSKeyedUnarchiver正在自动释放它:

GameSettings *gGameData = [NSKeyedUnarchiver unarchiveObjectWithData:data];

Fix: 固定:

GameSettings *gGameData = [[NSKeyedUnarchiver unarchiveObjectWithData:data] retain];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM