我尝试了以下方法,但它不起作用。我需要使当前得分NSInteger
等于registerScore中的得分参数。任何提示或建议将不胜感激。
+ (void)registerScore:(NSInteger)score
{
[Score bestScore] = score;
}
+ (NSInteger) bestScore
{
return self;
}
这是别人做的,但我不想使用NSUserDefaults
,因为不需要保存数据。
+ (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;
}
答案 0 :(得分:1)
正如我在评论中所说,这是一个例子。
<强> 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
并使用它:
[[Score sharedScore] setBestScore:15];
NSLog(@"%d", [[Score sharedScore] bestScore]);
答案 1 :(得分:0)
如果您想在每次注册分数时更新currentScore
,以下内容将有效。
@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
编辑:更新了有关不保存数据的新请求的答案。