gpt4 book ai didi

inheritance - 如何使 NSCoding 与对象继承一起工作?

转载 作者:行者123 更新时间:2023-12-04 05:38:58 25 4
gpt4 key购买 nike

我正在尝试使用基本的父子对象继承系统编写一个简单的 RPG,该系统被保存并加载到游戏状态中,但是当发生加载时,父级没有数据出现,我正在努力理解为什么。

为了简单起见,我做了一个简单的父子对象继承,其中,
Player继承自父级 Character
我把所有东西都放在一个数组中( listOfPlayers ),然后归档对象;当我来加载数据时,Character对象不再存在。

我已登录到控制台以下内容;

METHOD: saveGameState()
2012-07-17 20:12:24.762 TestState[72342:40b] self.listOfPlayers (1)
2012-07-17 20:12:24.762 TestState[72342:40b] p.name = BOB
2012-07-17 20:12:24.763 TestState[72342:40b] p.xp = 1000

METHOD: loadGameState()
2012-07-17 20:12:27.098 TestState[72342:40b] Last Save Date = 2012-07-17 19:12:24 +0000
2012-07-17 20:12:27.099 TestState[72342:40b] self.listOfPlayers (1)
2012-07-17 20:12:27.100 TestState[72342:40b] p.name = (null)
2012-07-17 20:12:27.101 TestState[72342:40b] p.xp = 1000
2012-07-17 20:12:27.102 TestState[72342:40b] State:Loaded

保存状态似乎正在保存名称( Character 模型的一部分)但是
那么负载状态的名称似乎是 null
我不确定我做错了什么。

我的代码现在如下;
// Inside my app delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Singleton
GameStateManager *state = [GameStateManager sharedGameStateManager];
[state setupGameState];
return YES;
}

// Character.h
@interface Character : NSObject
<NSCoding>
{
NSString *name;
}

@property (nonatomic, retain) NSString *name;

@end

// Character.m
@implementation Character
@synthesize name;

#pragma mark - NSCoding

-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];

if (self)
{
NSLog(@"Loading character object");
self.name = [aDecoder decodeObjectForKey:@"name"];
}

return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
// Archive the object
NSLog(@"Archiving character object");

[aCoder encodeObject:self.name forKey:@"name"];
}
@end


// Player.h
#import "Character.h"

@interface Player : Character
<NSCoding>
{
NSNumber *xp; // xp points
}
@property (nonatomic, retain) NSNumber *xp;

+(NSMutableArray *) createPlayers;
@end

// Player.m
@implementation Player
@synthesize xp;

- (id)init {
self = [super init];
if (self) {
}
return self;
}

+(NSMutableArray *) createPlayers
{
NSMutableArray *list = [NSMutableArray array];

Player *p1 = [[Player alloc] init];
[p1 setXp:[NSNumber numberWithInt:1000]];
[p1 setName:@"BOB"];
[list addObject:p1];
[p1 release];

return list;
}


#pragma mark - NSCoding

-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];

if (self)
{
self.xp = [aDecoder decodeObjectForKey:@"xp"];
}

return self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{
// Archive the object
[aCoder encodeObject:self.xp forKey:@"xp"];
}
@end

// GameStateManager.m file
-(void)saveGameState
{
NSLog(@"METHOD: saveGameState()");

self.lastSaveDate = [NSDate date];

NSLog(@"self.listOfPlayers (%d)", [self.listOfPlayers count]);


for (Player *p in self.listOfPlayers) {
NSLog(@"p.name = %@", p.name);
NSLog(@"p.xp = %d", [p.xp intValue]);
}

NSString *file = kGameSaveFile;
NSString *documentsDirectory = [MyHelpers getDocumentsDirectory];
NSString *gameStatePath = [documentsDirectory stringByAppendingPathComponent:file];
BOOL result;

NSLog(@"gameStatePath = %@", gameStatePath);

// Set up the encoder and storage for the game state data
NSMutableData *gameData;
NSKeyedArchiver *encoder;
gameData = [NSMutableData data];
encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:gameData];

// Archive the object
[encoder encodeObject:self.lastSaveDate forKey:@"lastSaveDate"];
[encoder encodeObject:self.listOfPlayers forKey:@"listOfPlayers"];

// Finish encoding and write to disk
[encoder finishEncoding];
result = [gameData writeToFile:gameStatePath atomically:YES];
[encoder release];

if (result == YES)
{
NSLog(@"Result = OK");
} else {
NSLog(@"Result = BAD");
}
}

-(void)loadGameState
{
NSLog(@"METHOD: loadGameState()");

NSString *file = kGameSaveFile;
NSString *documentsDirectory = [MyHelpers getDocumentsDirectory];
NSString *gameStatePath = [documentsDirectory stringByAppendingPathComponent:file];


NSMutableData *gameData;
NSKeyedUnarchiver *decoder;
gameData = [NSData dataWithContentsOfFile:gameStatePath];

// Check to see if the .dat file exists, and load contents
if (gameData)
{
decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:gameData];

self.lastSaveDate = [[decoder decodeObjectForKey:@"lastSaveDate"] retain];
self.listOfPlayers = [[decoder decodeObjectForKey:@"listOfPlayers"] retain];

[decoder finishDecoding];
[decoder release];

NSLog(@"Last Save Date = %@", self.lastSaveDate);
NSLog(@"self.listOfPlayers (%d)", [self.listOfPlayers count]);


for (Player *p in self.listOfPlayers) {
NSLog(@"p.name = %@", p.name);
NSLog(@"p.xp = %d", [p.xp intValue]);
}

} else {

NSLog(@"No game data!");
assert(-1);
abort();

}


NSLog(@"State:Loaded");
}

编辑,

我认为它必须有 if ( (self = [super initWithCoder:aDecoder]) )[super encodeWithCoder:aCoder];

最佳答案

使用

 if ( (self = [super initWithCoder:aDecoder]) ) and [super encodeWithCoder:aCoder];

帮助我解决了这个问题,还有这个 question有答案也有帮助。

谢谢

关于inheritance - 如何使 NSCoding 与对象继承一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11529515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com