使用 NSKeyArchiver 保存数据,但是当我使用 NSKeyUnarchiver 加载数据时,它全是 0 或 nil
我正在尝试使用 NSKeyedArchiver 保存称为 blob 的对象。 我使用 NSMutableData 和 NSKeyedArchiver 将数据保存到表示该对象的文件中。然后我使用 NSData 和 NSKeyedUnarchiver 从文件中获取数据。
它似乎工作得很好,没有错误,但是当我加载它时,所有属性都是“0”或nil。
这是为什么呢?
-(void) LoadBlobs
{
[blobs release];
blobs = nil;
blobs = [[NSMutableArray alloc]init];
NSNumber *blobAmounts = [[NSUserDefaults standardUserDefaults]objectForKey:blobAmount];
if([blobAmounts intValue] <= 0)
{
return;
}
for(int i = 0; i < [blobAmounts intValue]; i++)
{
NSLog(@"Loading Blob #%i...", i);
NSString *curBlobDirectory = [[NSString alloc]initWithFormat:@"%@%i", saveDirectoryBlobs, i];
NSLog(@"Blob directory is: %@", curBlobDirectory);
NSData *blobData = [[NSData alloc]initWithContentsOfFile:curBlobDirectory];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:blobData];
Blob *b = [[Blob alloc]initWithFile:@"Blob.png"];
b.position = CGPointMake([[unarchiver decodeObjectForKey:@"x"]intValue], [[unarchiver decodeObjectForKey:@"y"]intValue]);
b.health = [[unarchiver decodeObjectForKey:@"health"]intValue];
b.mating = [[unarchiver decodeObjectForKey:@"mating"]intValue];
b.aiEntity = [[unarchiver decodeObjectForKey:@"entity"]intValue];
b.action = [[unarchiver decodeObjectForKey:@"action"]intValue];
b.faction = [[unarchiver decodeObjectForKey:@"faction"]intValue];
b.equippedHelmet = [[unarchiver decodeObjectForKey:@"helmet"]boolValue];
b.equippedSpear = [[unarchiver decodeObjectForKey:@"spear"]boolValue];
b.goingTo = CGPointMake([[unarchiver decodeObjectForKey:@"gx"]intValue], [[unarchiver decodeObjectForKey:@"gy"]intValue]);
b.homeID = [[unarchiver decodeObjectForKey:@"home"]intValue];
b.aiID = [[unarchiver decodeObjectForKey:@"ai"]intValue];
b.iD = [[unarchiver decodeObjectForKey:@"id"]intValue];
NSLog(@"Blob #%i properties are: %f, %i, %i, %i, %i, %i, %i", i, b.health, b.mating, b.aiEntity, b.faction, b.action, b.equippedHelmet, b.equippedSpear);
[blobs addObject:b];
[self addChild:b z:1];
[unarchiver finishDecoding];
[blobData release];
[curBlobDirectory release];
[unarchiver release];
}
NSLog(@"%i blobs successfully loaded!", [blobs count]);
}
-(void) SaveBlobs
{
[[NSUserDefaults standardUserDefaults]setValue:[NSNumber numberWithBool:true] forKey:gameSavedPrev];
int i = 0;
for(Blob *b in blobs)
{
NSLog(@"Saving Blob #%i...", i);
NSString *curBlobDirectory = [[NSString alloc]initWithFormat:@"%@%i", saveDirectoryBlobs, i];
NSMutableData *blobData = [[NSMutableData alloc]init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:blobData];
[archiver encodeInt:b.position.x forKey:@"x"];
[archiver encodeInt:b.position.y forKey:@"y"];
[archiver encodeInt:b.health forKey:@"health"];
[archiver encodeInt:b.mating forKey:@"mating"];
[archiver encodeInt:b.aiEntity forKey:@"entity"];
[archiver encodeInt:b.action forKey:@"action"];
[archiver encodeInt:b.faction forKey:@"faction"];
[archiver encodeBool:b.equippedHelmet forKey:@"helmet"];
[archiver encodeBool:b.equippedSpear forKey:@"spear"];
[archiver encodeInt:b.position.x forKey:@"gx"];
[archiver encodeInt:b.position.y forKey:@"gy"];
[archiver encodeInt:b.homeID forKey:@"home"];
[archiver encodeInt:b.aiID forKey:@"ai"];
[archiver encodeInt:b.iD forKey:@"id"];
[archiver finishEncoding];
BOOL success = [blobData writeToFile:curBlobDirectory atomically:YES];
NSLog(@"Blob directory is: %@", curBlobDirectory);
[archiver release];
[blobData release];
[curBlobDirectory release];
i++;
}
}
I'm trying to save the objects called blobs using NSKeyedArchiver.
I use NSMutableData and NSKeyedArchiver to save the data onto a file that represents that object. then I use NSData and NSKeyedUnarchiver to get the data from the files.
It seems to work out fine with no errors but when I load it all of the properties are "0" or nil.
Why is this?
-(void) LoadBlobs
{
[blobs release];
blobs = nil;
blobs = [[NSMutableArray alloc]init];
NSNumber *blobAmounts = [[NSUserDefaults standardUserDefaults]objectForKey:blobAmount];
if([blobAmounts intValue] <= 0)
{
return;
}
for(int i = 0; i < [blobAmounts intValue]; i++)
{
NSLog(@"Loading Blob #%i...", i);
NSString *curBlobDirectory = [[NSString alloc]initWithFormat:@"%@%i", saveDirectoryBlobs, i];
NSLog(@"Blob directory is: %@", curBlobDirectory);
NSData *blobData = [[NSData alloc]initWithContentsOfFile:curBlobDirectory];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:blobData];
Blob *b = [[Blob alloc]initWithFile:@"Blob.png"];
b.position = CGPointMake([[unarchiver decodeObjectForKey:@"x"]intValue], [[unarchiver decodeObjectForKey:@"y"]intValue]);
b.health = [[unarchiver decodeObjectForKey:@"health"]intValue];
b.mating = [[unarchiver decodeObjectForKey:@"mating"]intValue];
b.aiEntity = [[unarchiver decodeObjectForKey:@"entity"]intValue];
b.action = [[unarchiver decodeObjectForKey:@"action"]intValue];
b.faction = [[unarchiver decodeObjectForKey:@"faction"]intValue];
b.equippedHelmet = [[unarchiver decodeObjectForKey:@"helmet"]boolValue];
b.equippedSpear = [[unarchiver decodeObjectForKey:@"spear"]boolValue];
b.goingTo = CGPointMake([[unarchiver decodeObjectForKey:@"gx"]intValue], [[unarchiver decodeObjectForKey:@"gy"]intValue]);
b.homeID = [[unarchiver decodeObjectForKey:@"home"]intValue];
b.aiID = [[unarchiver decodeObjectForKey:@"ai"]intValue];
b.iD = [[unarchiver decodeObjectForKey:@"id"]intValue];
NSLog(@"Blob #%i properties are: %f, %i, %i, %i, %i, %i, %i", i, b.health, b.mating, b.aiEntity, b.faction, b.action, b.equippedHelmet, b.equippedSpear);
[blobs addObject:b];
[self addChild:b z:1];
[unarchiver finishDecoding];
[blobData release];
[curBlobDirectory release];
[unarchiver release];
}
NSLog(@"%i blobs successfully loaded!", [blobs count]);
}
-(void) SaveBlobs
{
[[NSUserDefaults standardUserDefaults]setValue:[NSNumber numberWithBool:true] forKey:gameSavedPrev];
int i = 0;
for(Blob *b in blobs)
{
NSLog(@"Saving Blob #%i...", i);
NSString *curBlobDirectory = [[NSString alloc]initWithFormat:@"%@%i", saveDirectoryBlobs, i];
NSMutableData *blobData = [[NSMutableData alloc]init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:blobData];
[archiver encodeInt:b.position.x forKey:@"x"];
[archiver encodeInt:b.position.y forKey:@"y"];
[archiver encodeInt:b.health forKey:@"health"];
[archiver encodeInt:b.mating forKey:@"mating"];
[archiver encodeInt:b.aiEntity forKey:@"entity"];
[archiver encodeInt:b.action forKey:@"action"];
[archiver encodeInt:b.faction forKey:@"faction"];
[archiver encodeBool:b.equippedHelmet forKey:@"helmet"];
[archiver encodeBool:b.equippedSpear forKey:@"spear"];
[archiver encodeInt:b.position.x forKey:@"gx"];
[archiver encodeInt:b.position.y forKey:@"gy"];
[archiver encodeInt:b.homeID forKey:@"home"];
[archiver encodeInt:b.aiID forKey:@"ai"];
[archiver encodeInt:b.iD forKey:@"id"];
[archiver finishEncoding];
BOOL success = [blobData writeToFile:curBlobDirectory atomically:YES];
NSLog(@"Blob directory is: %@", curBlobDirectory);
[archiver release];
[blobData release];
[curBlobDirectory release];
i++;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
替换所有类似的内容
如果
您使用
encodeInt:forKey:
,则必须使用decodeIntForKey:
。如果您使用
encodeBool:forKey:,则必须使用
decodeBoolForKey:`。等等
replace everything that is like
with
If you use
encodeInt:forKey:
you have to usedecodeIntForKey:
.If you use
encodeBool:forKey: you have to use
decodeBoolForKey:`.and so on