所以我有一个包含一些自定义类的数组:
带有游戏的 NSMutableArray (FirstViewcontroller) -带有骰子的游戏 (Game.m) -- 带有选项的骰子 (Dices.m)
在每个自定义类中,我设置了编码和解码:
-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:[self _gameNaam] forKey:@"_gameNaam"];
}
-(id)initWithCoder:(NSCoder *)aDecoder{
if (!(self = [super init]))
return nil;
[self set_gameNaam:[aDecoder decodeObjectForKey:@"_gameNaam"]];
return self;
}
在第一个视图控制器中,我正在处理加载和保存。但是解码器总是返回空数组。所以里面什么都没有。这是我的代码:
- (void) saveToFile{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *bestand = [path stringByAppendingPathComponent:@"data.kets"];
if ([NSKeyedArchiver archiveRootObject:[self dobbelArray] toFile:bestand])
NSLog(@"Did it!!");
}
- (void) loadFromFile{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *bestand = [path stringByAppendingPathComponent:@"data.kets"];
if ([[NSFileManager defaultManager] fileExistsAtPath:bestand]){
if ([NSKeyedUnarchiver unarchiveObjectWithFile:bestand])
NSLog(@"Did it!!");
}
}
-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:[self dobbelArray] forKey:@"dobbelArray"];
}
-(id)initWithCoder:(NSCoder *)aDecoder{
if (!(self = [super initWithCoder:aDecoder]))
return nil;
[self setDobbelArray:[aDecoder decodeObjectForKey:@"dobbelArray"]];
return self;
}
不确定我做错了什么?在- (void)viewDidLoad
FirstViewController 中调用加载。在[NSMutableArray new];
我的数组之后。UnwindSegue
从我的 addViewController返回 grom 时完成保存。
有任何想法吗?
亲切的问候