0
我有saveing得分類:解碼器的NSString墜毀
#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字符串]:無法識別的選擇發送到實例0x544dd10 [4011:207] *終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因: ' - [NSCFString字符串]:無法識別的選擇發送到實例0x544dd10'
任何人都可以幫助我離開這裏。謝謝^ _^