2013-10-26 97 views
0

我有一個名爲HeroState的類,它是從名爲CharacterProperties的另一個類的繼承,它包含armorId,headId,earsId和一些其他創建字符的常見值。在這個我已經在頭上宣佈的新班級中。 (它負責的球員進步的階級,通過說這是一個單身)編碼和解碼GameState類問題

@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 

而且我有負責保存和加載類的「數據庫」級。

數據庫頭:

#import <Foundation/Foundation.h> 

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

數據庫實現:

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]; 
} 

但是當我用我的HeroClass

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

這裏保存

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

它給了我一個錯誤,當我嘗試建立它:

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)

我有一個的initWithCoder功能和encodeWithCoder功能堅守NSCoding的協議。那麼有什麼可能是錯的?我將這個類作爲一個NSObject inhertied類,並且對它沒有任何問題,但是當我決定重新創建它時,我的CharacterProperties類的繼承是NSObject繼承的問題。

我會很感激任何類型的幫助!

回答

0

您確定要將這些函數聲明爲C函數嗎?

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

也許你應該把它們聲明爲靜態類函數。

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

並據此稱之爲:

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

PS:請開始使用ARC。沒有理由不在這個時代。

+0

我曾考慮過但從來沒有想過這將是這個問題的原因,它是。更改我的數據庫類是一個客觀的C類,它沒有建立問題,謝謝Steffen。關於PS:我將開始使用ARC,謝謝你的提示。 –