I have a class for saveing score:
#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
And in my GameLayer I call to save score like this:
@interface GameLayer : CCLayer
{
ScoreData *scoreData;
}
-(void)gameOver
{
scoreData.playerName = @"test";
scoreData.playDate = [NSDate new];
[[GameDataManager sharedGameDataManager] updateLocalScore:scoreData];
}
And the code to save the data:
-(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;
}
And the data was saved correctly...
Then I tried to read the data from plist:
-(BOOL) readApplicationData:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirect开发者_Python百科ory 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;
}
But the app crashed here:
- (id)initWithCoder:(NSCoder *)decoder
{
ScoreData *highScoreData = [[ScoreData alloc] init];
highScoreData.playerName = [[decoder decodeObjectForKey:kPlayerNameKey] string];
return highScoreData;
}
Saying: [4011:207] -[NSCFString string]: unrecognized selector sent to instance 0x544dd10 [4011:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString string]: unrecognized selector sent to instance 0x544dd10'
Can anybody help me out of here. Thanks^_^
Well, the error message says it all. You are calling a -string
method on NSString
and since such a method does not exist, your app crashes. Moreover, the -string
and -date
messages there are completely unnecessary. Just remove them.
There are more problems in your code: for example: you should generally not alloc a new object in -initWithCoder:
. If you do, you have a memory leak. The method should look like this:
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super init];
if (self != nil) {
self.playerName = [decoder decodeObjectForKey:kPlayerNameKey];
self.playDate = [decoder decodeObjectForKey:kPlayDateKey];
}
return self;
}
I haven't checked the rest of your code so it's very possible there are more bugs in it.
精彩评论