Pergunta

I save Accounts with the NSKeyArchiver to a file, but when I try to load them, my App just crashes saying that Account doesn't implement initWithCoder: even though it does and it also conforms to NSCoding:

-[Account initWithCoder:]: unrecognized selector sent to instance 0xa947a00

Account.m

 - (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
    _name = [aDecoder decodeObjectForKey:@"name"];
    _accessToken = [aDecoder decodeObjectForKey:@"accessToken"];
    _refreshToken = [aDecoder decodeObjectForKey:@"refreshToken"];
    _selected = [aDecoder decodeBoolForKey:@"selected"];
    lastUpdate = [aDecoder decodeObjectForKey:@"lastUpdate"];
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeObject:_accessToken forKey:@"accessToken"];
[aCoder encodeObject:_refreshToken forKey:@"refreshToken"];
[aCoder encodeBool:_selected forKey:@"selected"];
[aCoder encodeObject:lastUpdate forKey:@"lastUpdate"];
}

AccountStore.m And here I load them:

NSString *path = [[self archivePath] stringByAppendingPathComponent:@"accounts.archive"];
accounts = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
if (!accounts) {
    accounts = [[NSMutableArray alloc] init];
}

Also I should mention that it already worked, but from one second to an other it didn't anymore. And I checked whether NSKeyedArchiver saved correctly: It does.

Foi útil?

Solução

The problem might be a name clash.

Apple's Message.framework contains a class named Account. While you would never link this private framework directly it could be loaded indirectly from other Apple frameworks, for example MessageUI.framework. I can imagine you recently added this framework to your project. This would be when your bug first occurred.

The only solution is to rename your Account class. There are actually quite a lot of unprefixed classes in Apple's frameworks, so you should use a name prefix anyway.

And you should file a bug, as proposed by @bbum here.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top