-2

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;
}
Larme
  • 24,190
  • 6
  • 51
  • 81
  • Can you post some more code, is there a `[Score setBestScore:]`? You can't return `self` from `bestScore`, and you can't assign a value to a class method getter! – Rich Apr 18 '14 at 10:39
  • what is self? it's a static method (+) so self is the class itself, is it an NSObject or what? how can it be an integer? – meronix Apr 18 '14 at 10:39
  • You need object method instead of class method. Make a singleton class set its variables `@property` and Objective-C do setter getter by default. – TheTiger Apr 18 '14 at 10:45
  • are you storing the best score temporarily or it need to be shown every time the app opens – Prashanth Apr 18 '14 at 10:47
  • @user3435527 can you give some more context to your question, I've answer it best I could but your question is very vague. – Rich Apr 18 '14 at 10:50

2 Answers2

1

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]);
TheTiger
  • 13,264
  • 3
  • 57
  • 82
  • 1
    If we consider using this shared instance from different threads, shouldn't we make the `bestScore` an `atomic` property? – FreeNickname Apr 18 '14 at 11:20
  • Actually I read http://stackoverflow.com/questions/588866/whats-the-difference-between-the-atomic-and-nonatomic-attributes ... the @property is atomic by default – TheTiger Apr 18 '14 at 11:40
0

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.

Rich
  • 8,108
  • 5
  • 46
  • 59
  • Why are you using a static global variable? It will be shared by all the instances of the class, won't it? – FreeNickname Apr 18 '14 at 10:51
  • All his methods are class methods anyway. He wants a drop in solution to replace using `NSUserDefaults`. – Rich Apr 18 '14 at 10:52
  • Appreciate it, I approved your answer, but I want to know/learn why this works. I never seen this used before. Is it because the NSInterger is global and can access throughout the class? – user3435527 Apr 18 '14 at 11:00
  • @user3435527 which specific bit? – Rich Apr 18 '14 at 11:00
  • Also, I tried doing this in the inside the implementation but it didn't work. The error was this "Instance variable currentscore accessed in class method" I guess class methods (+) can't use instance variables? – user3435527 Apr 18 '14 at 11:04
  • 1
    @user3435527 This is wrong way to store the variables. Check my answer. And if it doesn't work then why you accepted ? – TheTiger Apr 18 '14 at 11:08
  • That is correct - you don't actually need to define them as `static` that's more for clarity on what they are. – Rich Apr 18 '14 at 11:08
  • @TheTiger there are good times to use a singleton, and other times not. This is not a _wrong_ way to store the **values**, but a different way. – Rich Apr 18 '14 at 11:10
  • use ** values ** instead of tag... **:)** – TheTiger Apr 18 '14 at 11:10
  • @Rich And if you don't define them `static` they can not be use in class methods. – TheTiger Apr 18 '14 at 11:12
  • @TheTiger I've also just updated it to log out the values http://ideone.com/c0Jo15 – Rich Apr 18 '14 at 11:17
  • Yes just because of they are free to use any where in this class. But generally we don't declare variables like that. – TheTiger Apr 18 '14 at 11:18
  • "And if you don't define them static they can not be use in class methods." I was actually correcting that comment which is wrong. There is nothing wrong with using the above approach, it is certainly better than the over use of a singleton for something as trival as this in my opinion. – Rich Apr 18 '14 at 11:22
  • 1
    I agree with Rich. I do not want to create a singleton pattern for one specific value that I am only going to use in ONE class. If I had multiple classes I may understand using the singleton pattern. – user3435527 Apr 18 '14 at 12:14
  • @user3435527 If you want to use it in single class then its easier. Do same thing which I did in score class and use `self` instead of `[Score sharedScore]`. – TheTiger Apr 18 '14 at 13:11