我有一个对象,我使用 NSKeyedArchiver 将其转换为 NSData,然后将其存储到 NSUserDefaults 中。除了对象拥有的数组元素之外,所有内容都被正确保存。数组中的所有对象都符合 NSCoder 协议(或任何他们所谓的 - 例如self.property = [decoder decodeObjectForKey:@"key"] and [encoder encodeObjectForKey:@"key"]
)
当我保存对象时,数组的元素保留在数组中,但它们的属性本身不会被保存。我确实调用了该sycnrhonize
方法,所以这不是问题。
请注意,所有其他时间我保存并加载它是正确的,它只是不保存属于对象的数组的元素。我必须单独保存吗?
“当前状态” NSNumber 没有被保存。正在保存目标和目标
导入“级别.h”
@implementation Level
@synthesize objective = _objective;
@synthesize isComplete = _isComplete;
@synthesize goldReward = _goldReward;
@synthesize xpReward = _xpReward;
@synthesize missionID = _missionID;
@synthesize currentStatus = _currentStatus;
@synthesize targetName = _targetName;
@synthesize owner = _owner;
-(void)dealloc{
[super dealloc];
}
-(id)initWithMissionID:(int)number{
if (self = [super init]) {
self.currentStatus = 0;
self.isComplete = NO;
self.missionID = [NSNumber numberWithInt:number];
[self setUpMisson];
}
return self;
}
-(void)setUpMisson{
if ([self.missionID intValue] == 0) {
self.xpReward = [NSNumber numberWithInt:100];
self.goldReward = [NSNumber numberWithInt:100];
self.objective = [NSNumber numberWithInt:3];
self.targetName = @"Swordsman";
CCLOG(@"Gotta kill some swordsmen!");
}
}
-(void)encodeWithCoder:(NSCoder *)encoder{
[encoder encodeObject:self.objective forKey:@"objective"];
[encoder encodeObject:self.isComplete forKey:@"isComplete"];
[encoder encodeObject:self.goldReward forKey:@"goldReward"];
[encoder encodeObject:self.xpReward forKey:@"xpReward"];
[encoder encodeObject:self.missionID forKey:@"missionID"];
[encoder encodeObject:self.currentStatus forKey:@"currentStatus"];
[encoder encodeObject:self.targetName forKey:@"targetName"];
[encoder encodeObject:self.owner forKey:@"owner"];
CCLOG(@"SAVING LEVEL");
}
-(id)initWithCoder:(NSCoder *)decoder{
if (self = [super init]) {
self.objective = [[decoder decodeObjectForKey:@"objective"]retain];
self.isComplete = [[decoder decodeObjectForKey:@"isComplete"]retain];
self.goldReward = [[decoder decodeObjectForKey:@"goldReward"]retain];
self.xpReward = [[decoder decodeObjectForKey:@"xpReward"]retain];
self.missionID = [[decoder decodeObjectForKey:@"missionID"]retain];
self.targetName = [[decoder decodeObjectForKey:@"targetName"]retain];
self.owner = [[decoder decodeObjectForKey:@"owner"]retain];
CCLOG(@"LOADING LEVEL");
}
return self;
}
-(void)updateStatusForKill:(AI *)killedTarget{
CCLOG(@"WE KILLED: %@ and OUR GOAL IS: %@",killedTarget.name,self.targetName);
if ([killedTarget.name isEqualToString:self.targetName]) {
[self setCurrentStatus:[NSNumber numberWithInt:[self.currentStatus intValue]+1]];
CCLOG(@"Current Status: %i Objective: %i", [self.currentStatus intValue],[self.objective intValue]);
if ([self.currentStatus intValue] == [self.objective intValue]) {
[self completeMission];
}
}
}
-(void)completeMission{
[self.owner setCoins:[NSNumber numberWithInt:[[self.owner coins]intValue] + [self.goldReward intValue]]];
[self.owner setXp:[NSNumber numberWithInt:[[self.owner xp]intValue] + [self.xpReward intValue]]];
CCLOG(@"complete");
[[self.owner missionList]removeObject:self];
}
@end
编辑:“所有者”指的是被保存的对象。我认为这就是问题所在,所以我将其删除并再次测试。
编辑:那什么也没做!