[英]What is the best way to make an NSInteger return method equal NSInteger parameter?
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.我需要使当前分数NSInteger
等于 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.别人就是这样做的,但我不想使用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;
}
As I have told in my comment here is the example.正如我在我的评论中所说的那样,这是一个例子。
Score.h分数.h
#import <Foundation/Foundation.h>
@interface Score : NSObject
+(Score *)sharedScore;
@property (nonatomic) NSInteger bestScore;
@end
Score.m分数.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]);
If you want to update the currentScore
each time you register a score the following will work.如果您想在每次注册分数时更新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
EDIT: Updated answer with new request about not saving data.编辑:用关于不保存数据的新请求更新了答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.