我使用 NSKeyArchiver 将 Accounts 保存到一个文件中,但是当我尝试加载它们时,我的应用程序只是崩溃了,说 Account 没有实现 initWithCoder: 即使它实现了并且它也符合 NSCoding:
-[Account initWithCoder:]: unrecognized selector sent to instance 0xa947a00
帐户.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 我在这里加载它们:
NSString *path = [[self archivePath] stringByAppendingPathComponent:@"accounts.archive"];
accounts = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
if (!accounts) {
accounts = [[NSMutableArray alloc] init];
}
另外我应该提到它已经起作用了,但是从一秒钟到另一秒钟它就不再起作用了。我检查了 NSKeyedArchiver 是否正确保存:确实如此。