Question

I tried doing the following method ,but it doesn't work. I need make the current score NSInteger to equal the score parameter in registerScore. Any tips or suggestions will be appreciated.

+ (void)registerScore:(NSInteger)score 
{
    [Score bestScore] = score;
}

+ (NSInteger) bestScore 
{
    return self;
}

This is how someone else did it, but I don't want to use NSUserDefaults because the data doesn't need to be saved.

+ (void)registerScore:(NSInteger)score
{
    [Score setBestScore:score];
}

+ (void) setBestScore:(NSInteger) bestScore
{
    [[NSUserDefaults standardUserDefaults] setInteger:bestScore forKey:kBestScoreKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSInteger) bestScore
{
    return [[NSUserDefaults standardUserDefaults] integerForKey:kBestScoreKey];
}

+ (NSInteger) currentScore
{
    return self;
}
Was it helpful?

Solution 2

If you want to update the currentScore each time you register a score the following will work.

@implementation Score

    static NSInteger currentScore;
    static NSInteger bestScore;

    + (void)registerScore:(NSInteger)score
    {
        currentScore = score;
        [Score setBestScore:score];
    }

    + (void) setBestScore:(NSInteger)score
    {
        if (score > bestScore) {
            bestScore = score;
        }
    }

    + (NSInteger) bestScore
    {
        return bestScore;
    }

    + (NSInteger) currentScore{
        return currentScore;
    }
@end

EDIT: Updated answer with new request about not saving data.

OTHER TIPS

As I have told in my comment here is the example.

Score.h

#import <Foundation/Foundation.h>

@interface Score : NSObject

+(Score *)sharedScore;

@property (nonatomic) NSInteger bestScore;

@end

Score.m

#import "Score.h"

@implementation Score

static Score *score = nil;
+(Score *)sharedScore
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        score = [[Score alloc] init];
    });

    return score;
}

@end

And use it like:

[[Score sharedScore] setBestScore:15];
NSLog(@"%d", [[Score sharedScore] bestScore]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top