0

スコアを保存するためのクラスがあります:

#import "cocos2d.h"

@interface ScoreData : NSObject<NSCoding> {

    NSString *playerName;
    NSDate *playDate;
}
-(NSString* )description;
@property (nonatomic, retain) NSString *playerName;
@property (nonatomic, retain) NSDate *playDate;
@end


#import "GameData.h"

@implementation ScoreData
@synthesize playerName;
@synthesize playDate;

#define kPlayerNameKey      @"PlayerName"
#define kPlayDateKey        @"playDate"

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

    }

    return self;
}

- (void) encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:self.playerName
                   forKey:kPlayerNameKey];
    [encoder encodeObject:self.playDate
                   forKey:kPlayDateKey];
}

- (id)initWithCoder:(NSCoder *)decoder 
{
    ScoreData *highScoreData = [[ScoreData alloc] init];
    highScoreData.playerName = [[decoder decodeObjectForKey:kPlayerNameKey] string];
    highScoreData.playDate = [[decoder decodeObjectForKey:kPlayDateKey] date];

    return highScoreData;
}

@end

そして私の GameLayer では、次のようにスコアを保存するために呼び出します:

@interface GameLayer : CCLayer 
{
    ScoreData *scoreData;
}

-(void)gameOver
{
    scoreData.playerName = @"test";
    scoreData.playDate = [NSDate new];

    [[GameDataManager sharedGameDataManager] updateLocalScore:scoreData];
}

そして、データを保存するコード:

-(void)updateLocalHighScore:(ScoreData *)scoreData
{
    [highScoreDataArray addObject:scoreData];

    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:self.highScoreDataArray
            forKey:@"LocalHighScoreData"];

    [self writeApplicationData:dic bwriteFileName:@"teste.plist"];
}

-(BOOL) writeApplicationData:(NSDictionary *)data 
              bwriteFileName:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    if (!documentsDirectory) {
        NSLog(@"Documents directory not found!");
        return NO;
    }

    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];

    NSMutableArray *a = [[NSMutableArray alloc] init];
    a = [data objectForKey:@"ScoreData"];

    NSMutableData *_data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:_data];          
    [archiver encodeObject:data forKey:@"GameData"];
    [archiver finishEncoding];
    [_data writeToFile:appFile atomically:YES];
    [archiver release];
    [data release];

    return YES;
}

そして、データは正しく保存されました...

次に、plist からデータを読み取ろうとしました。

-(BOOL) readApplicationData:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];

    if (myData == nil) {

        return NO;
    }

    NSKeyedUnarchiver *un = [[NSKeyedUnarchiver alloc] initForReadingWithData:myData];
    NSMutableDictionary *dic = [un decodeObjectForKey:@"GameData"];

    self.highScoreDataArray = [dic objectForKey:@"ScoreData"];

    [un finishDecoding];
    [un release];

    return YES; 
}

しかし、ここでアプリがクラッシュしました。

- (id)initWithCoder:(NSCoder *)decoder 
{
    ScoreData *highScoreData = [[ScoreData alloc] init];
    highScoreData.playerName = [[decoder decodeObjectForKey:kPlayerNameKey] string];

    return highScoreData;
}

発言: [4011:207] -[NSCFString string]: 認識されないセレクターがインスタンス 0x544dd10 に送信されました [4011:207] *キャッチされない例外 'NSInvalidArgumentException' が原因でアプリを終了します。理由: '-[NSCFString string]: 認識されないセレクターがインスタンス 0x544dd10 に送信されました'

誰か助けてくれませんか。ありがとう^_^

4

1 に答える 1