Objective-C NSUserDefaults:HighScoreManager,为什么这不起作用?

发布于 2024-09-11 23:05:46 字数 3723 浏览 4 评论 0原文

我的应用程序不会保存数据或加载数据,它只是崩溃,我想知道我在高分管理器类中做错了什么。

我将发布我的班级的 .h 和 .m 文件:

.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

我如何尝试使用该班级

    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];

尽管我“保存”了分数,但当我离开分数页面并返回时,它就在哪里应用程序崩溃。我不太确定我做错了什么或者为什么它没有保存


当它崩溃时我得到这个“_class_getMethodNoSuper_nolock”,有时是 objC_send 或类似的东西

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遥远的她 2024-09-18 23:05:46
  • 不要发布NSUD。您没有创建它,也没有保留它,因此您不拥有它。这可能会导致崩溃。

  • 您还应该保留实例变量。

  • [NSUD2 arrayForKey: @"game.score.names"] 将在开头返回 nil,因此 [[NSUD2 arrayForKey: @"game.score.names"] arrayByAddingObject: name] 也将为零。您总是将字典值设置为 nil。

  • (并避免像“an_”这样的变量名称。这会使您的代码难以阅读。)

  • Do not release NSUD. You didn't create it and you didn't retain it, so you don't own it. This might be causing the crash.

  • You also should retain your instance variables.

  • [NSUD2 arrayForKey: @"game.score.names"] will return nil at the beginning, so [[NSUD2 arrayForKey: @"game.score.names"] arrayByAddingObject: name] will also be nil. You're always setting the dictionary values to nil.

  • (And avoid variable names like "an_". It makes your code difficult to read.)

甜心 2024-09-18 23:05:46

准确描述崩溃发生的位置,也许我可以提供更多帮助。我至少发现一个问题:

return [[[self alloc] initWithScore: score_ name: name_] autorelease];

应该是

return [[[Score alloc] initWithScore: score_ name: name_] autorelease];

Describe exactly where the crash is happening and maybe I can be more help. I spotted at least one issue:

return [[[self alloc] initWithScore: score_ name: name_] autorelease];

Should be

return [[[Score alloc] initWithScore: score_ name: name_] autorelease];
放肆 2024-09-18 23:05:46

没有测试您的代码,但我注意到的第一件事是您的静态工厂方法,它尝试分配 self 而不是类 Score

我很确定您需要:

+ (Score *) score: (NSNumber *)score_ name: (NSString *)name_
{
     return [[[Score alloc] initWithScore: score_ name: name_] autorelease];
}

还有一件事。 Score 类中的属性被声明为分配,这意味着您不会保留获得的分数和名称,如果它们超出范围,它也会使您的应用程序崩溃。

Didn't test your code but the first thing I noticed is your static factory methods, which tries to alloc self instead of the class Score

I'm pretty sure you need:

+ (Score *) score: (NSNumber *)score_ name: (NSString *)name_
{
     return [[[Score alloc] initWithScore: score_ name: name_] autorelease];
}

One more thing. Your properties in the Score class are declared as assign, which means you do not retain the score and name you get, if they go out of scope it will also crashes your app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文