简体   繁体   English

编码和解码GameState类问题

[英]Encoding and Decoding GameState Class Issue

I have a class named HeroState that is an inheritance from another class named CharacterProperties that contains armorId, headId, earsId and some other common values to create a character. 我有一个名为HeroState的类,该类是从另一个名为CharacterProperties的类继承的,该类包含armourId,headId,earsId和一些其他用于创建角色的常用值。 In this new class I have declared on the header. 在这个新类中,我在标头上声明了。 (It is the class in charge of the players progress , by saying that It's a singleton) (这是负责球员进步的班级,说这是一个单身人士)

    @interface HeroState : CharacterProperties <NSCoding>  {
    ControlsType controlsType;
    characterJob charJob;
    MonsterJob monsterJob;
    } 


@property MonsterJob monsterJob;
@property characterJob charJob;
@property (readwrite)ControlsType controlsType;

+ (HeroState *) sharedInstance;
- (void)save;

@end

And I have a "database" class in charge of saving and loading the class. 我有一个“数据库”类,负责保存和加载该类。

database Header : 数据库头:

#import <Foundation/Foundation.h>

id loadData(NSString * filename);
void saveData(id theData, NSString *filename);

database Implementation : 数据库实现:

NSString * pathForFile(NSString *filename) {
    // 1
    NSArray *paths =
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                        NSUserDomainMask,
                                        YES);
    // 2
    NSString *documentsDirectory = [paths objectAtIndex:0];
    // 3
    return [documentsDirectory
            stringByAppendingPathComponent:filename];
}

id loadData(NSString * filename) {
    // 4
    NSString *filePath = pathForFile(filename);
    // 5
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        // 6
        NSData *data = [[[NSData alloc]
                         initWithContentsOfFile:filePath] autorelease];
        // 7
        NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc]
                                          initForReadingWithData:data] autorelease];
        // 8
        id retval = [unarchiver decodeObjectForKey:@"Data"];
        [unarchiver finishDecoding];
        return retval;
    }
    return nil;
}

void saveData(id theData, NSString *filename) {
    // 9
    NSMutableData *data = [[[NSMutableData alloc] init] autorelease];
    // 10
    NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc]
                                  initForWritingWithMutableData:data] autorelease];
    // 11
    [archiver encodeObject:theData forKey:@"Data"];
    [archiver finishEncoding];
    // 12
    [data writeToFile:pathForFile(filename) atomically:YES];
}

But then when I use it on my HeroClass 但是当我在HeroClass上使用它时

+(HeroState*)sharedInstance {
    @synchronized([HeroState class])
    {
        if(!sharedInstance) {
            sharedInstance = [loadData(@"HeroState") retain];
            if (!sharedInstance) {
                [[self alloc] initWithNewStats];
            }
        }
        return sharedInstance;
    }
    return nil;
}

And here for saving 还有这里保存

- (void)save {
    saveData(self, @"HeroState");
}

It gives me an error when I try to build it: 当我尝试构建它时,它给了我一个错误:

Undefined symbols for architecture i386: "loadData(NSString*)", referenced from: +[HeroState sharedInstance] in HeroState.o "saveData(objc_object*, NSString*)", referenced from: -[HeroState save] in HeroState.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation) 架构i386的未定义符号:“ loadData(NSString *)”,引用自:HeroState.o中的+ [HeroState sharedInstance]“ saveData(objc_object *,NSString *)”,引用自:HeroState.o ld中的-[HeroState save] :找不到体系结构i386 clang的符号:错误:链接器命令失败,退出代码为1(使用-v查看调用)

I do have a initWithCoder function and a encodeWithCoder function that stick to the protocols of the NSCoding. 我确实有一个坚持NSCoding协议的initWithCoder函数和encodeWithCoder函数。 So what could be wrong? 那怎么可能出问题了? I had this class as a NSObject inhertied class before and had no problem about it , but when I decided to recreate it being a inheritance of my CharacterProperties class that was an Inheritance of NSObject problems occurred. 我以前将该类作为NSObject继承的类,对此没有任何问题,但是当我决定重新创建它时,这是我的CharacterProperties类的继承,而这是NSObject的继承。

I will appreciate any type of help! 我将不胜感激任何帮助!

Are you certain you wanted to declare these as C functions? 您确定要将它们声明为C函数吗?

NSString * pathForFile(NSString *filename) {..}
id loadData(NSString * filename) {..}
void saveData(id theData, NSString *filename)  {..}

Perhaps you should rather declare them as static class functions. 也许您应该将它们声明为静态类函数。

+(id) loadData:(NSString *)filename {..}
// similar for the other C functions

And call it accordingly: 并相应地调用它:

sharedInstance = [[HeroState loadData:@"HeroState"] retain];

PS: please start using ARC. PS:请开始使用ARC。 There's no reason not to in this day and age. 在这个时代没有理由不这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM