Objective-C NSUserDefaults: HighScoreManager, why doesn't this work? -
My app will not save data or load data, it just crashes, I wonder what's wrong My high score manager class
I am going to post .h and .m file for my class:
.h
#import & lt; Foundation / Foundation.h & gt; @ Interface score: NSWeb {nssting * name; NSNumber * score; } @ Property (Assign, Non-Metric) NSString * Name; @ Property (Assign, Non-Metric) NSNumber * Score; + (Score *) Score: (NSNumber *) score_name: (NSString *) name_; - (id) initWithScore: (NSNumber *) score_name: (NSString *) name_; @end @interfas highscore manager: NSOJs {NSARRA * AN; NSArray * as_; Hair load; } - (zero) load; - (BOOL) no scaurus; - (NSARRA *) Sorted scoring; - Add (zero): (NSString *) Name Score: (NSN * *) score; @end
.m
#import "HighScoreManager.h" @ implementation score @ synthesis name, score; + (Score *) Score: (NSNumber *) Score_Name: (NSString *) name_ [Return [[Score light] initWithScore: score_ name: name_] autorelease]; } - (id) initWithScore: (NSNumber *) Score_Name: (NSString *) name_ {if ((self = [super init])) {score = score_; Name = name_; } Healthy return; } @end @implementation HighScoreManager - (id) init {if ((self = [super init])) {loaded = NO; } Healthy return; } - (zero) load {if (loaded!) {NSUserDefaults * NSUD = [NSUserDefaults standard user default]; [Synchronize NSD]; An_ = [NSUD arranger: @ "game.score.names"]; AS_ = [NSUD arrayfork: @ "game.score.scores"]; [NSD release]; Weighted = yes; }} - (BOOL) no sparse {{self-load}; Return [an_ count] & lt; 1; } - (NSARRA *) Sorted scoring {{self load}; NSMutableArray * scoresObj = [NSMutableArray array]; (NSUNTEGIER I = 0; I & lt; [A_GATE]; I ++) {[Score Obvious Adject: [Score Score: [AS-ObjectIndex: I] Name: [A_ObjectAntindex: I]]] ;; } NSSortDescriptor * sortDescriptor = [[[NSSortDescriptor alloc] initWithKey: @ "scores" climb: no] autorescope]; NSArray * Sort descriptor = [NSArray arrayWithObject: sortDescriptor]; Return [Scoring Horoscopes Wrapping Descriptors: Sort Descriptors]; } - (Zero) Add: (NSString *) Name Score: (NSN * *) Score {NSUser Defaults * NSD2 = [NSUser Defaults Standard User Defaults]; [NSD2 2 set object: [[NSUD2AERERFORICI: @ "GameSensor.name"] Arbading Object: Name] Farke: @ "GameCore.com"]; [NSUD2 set object: [[NSUD2 arrayfork: @ "game.score.scores"] arrayBeadingAbsus: score] for: @ "game.score.scores"]; [Synchronize nsud2]; } - (zero) Delok [[an_ release]; [AS_ release]; [Super DeLoc]; } @end
How did I try to use class
highscore manager * HSM1 = [HighScoreManager new]; [HSM1Ad: @ "Johannes" Score: [NSNumber Number With 1998]]; [HSM1Ad: @ "Johannes" Score: [NSNumber NumberAttah: 8723]]; [HSM1Ad: @ "Johanness" Score: [NSNumber NumberAttah: 3283]]; [HSM1Ad: @ "Johanness" Score: [NSNumber NumberHatah: 1763]]; [HSM1Ad: @ "Johanness" Score: [NSNumber NumberAttah: 99 31]]; HICOSOR MANAGER * HSM = [HCOSManager New]; // Check if there is also a high score if ([HSM no. Score]) {CCL abel * no scaurus yat = [CCL label labelthath: @ "There are no scores yet." FontName: @ "Helvetica" font size: 36]; NoScoresYet.position = CCP (win.width / 2, win.height / 2); [NoScores set collar: CCC3 (0, 0, 0)]; [Self-added child: no scours if]; } Else {// Oh look, there were some high scores! } [HSM release];
Even if I "save" scores, when I go away from the score page, and go back, this is where the app crashes, I'm not sure That's what I am doing or doing wrong.
I get this when it crashes "_class_getMethodNoSuper_nolock" and occasionally objC_send or something like that
-
NSUD
You have not made it and you have not made it, so you do not do it yourself, it can cause an accident. -
You should also keep your frequency variable
-
[NSUD2 arrayForKey: @ "Game.score.names"]
will return zero at the beginning, hence the[[NSUD2 arrayfory: @ "Game.score.names"] array containing the object: name]
will also be zero. You're always setting a dictionary value for zero. Avoid variable names like -
(and "an_". This makes it hard to read your code.)
Comments
Post a Comment