My app won't save the data or load the data, it just crashes, I wonder what I'm doing wrong in my High Score Manager class.
I'm going to post my class's .h and .m file:
.h
#import <Foundation/Foundation.h>
@interface Score : NSObject
{
NSString *name;
NSNumber *score;
}
@property (assign, nonatomic) NSString *name;
@property (assign, nonatomic) NSNumber *score;
+ (Score *) score: (NSNumber *)score_ name: (NSString *)name_;
- (id) initWithScore: (NSNumber *)score_ name: (NSString *)name_;
@end
@interface HighScoreManager : NSObject
{
NSArray *an_;
NSArray *as_;
BOOL loaded;
}
- (void) load;
- (BOOL) noScores;
- (NSArray *) sortedScores;
- (void) add: (NSString *)name score: (NSNumber *)score;
@end
.m
#import "HighScoreManager.h"
@implementation Score
@synthesize name, score;
+ (Score *) score: (NSNumber *)score_ name: (NSString *)name_
{
return [[[Score alloc] initWithScore: score_ name: name_] autorelease];
}
- (id) initWithScore: (NSNumber *)score_ name: (NSString *)name_
{
if((self = [super init]))
{
score = score_;
name = name_;
}
return self;
}
@end
@implementation HighScoreManager
- (id) init
{
if((self = [super init]))
{
loaded = NO;
}
return self;
}
- (void) load
{
if(!loaded) {
NSUserDefaults *NSUD = [NSUserDefaults standardUserDefaults];
[NSUD synchronize];
an_ = [NSUD arrayForKey: @"game.score.names"];
as_ = [NSUD arrayForKey: @"game.score.scores"];
[NSUD release];
loaded = YES;
}
}
- (BOOL) noScores
{
[self load];
return [an_ count] < 1;
}
- (NSArray *) sortedScores
{
[self load];
NSMutableArray *scoresObj = [NSMutableArray array];
for(NSUInteger i = 0; i < [an_ count]; i++)
{
[scoresObj addObject: [Score score: [as_ objectAtIndex: i] name: [an_ objectAtIndex: i]]];
}
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"score" ascending: NO] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
return [scoresObj sortedArrayUsingDescriptors: sortDescriptors];
}
- (void) add: (NSString *)name score: (NSNumber *)score
{
NSUserDefaults *NSUD2 = [NSUserDefaults standardUserDefaults];
[NSUD2 setObject: [[NSUD2 arrayForKey: @"game.score.names"] arrayByAddingObject: name] forKey: @"game.score.names"];
[NSUD2 setObject: [[NSUD2 arrayForKey: @"game.score.scores"] arrayByAddingObject: score] forKey: @"game.score.scores"];
[NSUD2 synchronize];
}
- (void) dealloc
{
[an_ release];
[as_ release];
[super dealloc];
}
@end
How I tried to use the class
HighScoreManager *HSM1 = [HighScoreManager new];
[HSM1 add: @"Johannes" score: [NSNumber numberWithInt: 1298]];
[HSM1 add: @"Johannes" score: [NSNumber numberWithInt: 8723]];
[HSM1 add: @"Johannes" score: [NSNumber numberWithInt: 3283]];
[HSM1 add: @"Johannes" score: [NSNumber numberWithInt: 1763]];
[HSM1 add: @"Johannes" score: [NSNumber numberWithInt: 9931]];
HighScoreManager *HSM = [HighScoreManager new];
// Check if there even are any high scores.
if([HSM noScores])
{
CCLabel *noScoresYet = [CCLabel labelWithString: @"There are no scores yet." fontName: @"Helvetica" fontSize: 36];
noScoresYet.position = ccp(win.width / 2, win.height / 2);
[noScoresYet setColor: ccc3(0, 0, 0)];
[self addChild: noScoresYet];
} else
{
// Oh look, there were some high scores!
}
[HSM release];
Even though I "save" the scores, when I navigate away from the Scores page, and go back, it's where the app crashes. I'm not really sure what I'm doing wrong or why it isn't saving
I get this when it crashes "_class_getMethodNoSuper_nolock" and sometimes objC_send or something like that