0

Just wanted to get a sanity check on this code for storing and retrieving user data for iOS 7. It's not working but I'm not seeing the issue.

GameData.h

#import <Foundation/Foundation.h>

@interface GameData : NSObject<NSCoding>
{
    int HighScoreGD;
}

@property (readwrite,assign) int HighScoreGD;

@end

extern GameData *gGameData;

GameData.m

#import "GameData.h"

GameData *gGameData;


@implementation GameData

@synthesize HighScoreGD;

-(void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeInt:HighScoreGD forKey:@"HighScoreGD"];
}


-(id)initWithCoder:(NSCoder *)coder {
    if((self = [super init])) {
        HighScoreGD = [coder decodeIntForKey:@"HighScoreGD"];    
    }
    return self;
}

-(id) init {
    if((self = [super init])) {    
    }
    return self;
 }

 -(void) dealloc {
    //[super dealloc];
 }

 @end

Storing data:

gGameData.HighScoreGD = pointsHigh;

Retrieving data:

pointsHigh = gGameData.HighScoreGD;

I had used this in a bunch of old game concepts (cocos2d) in the past and it worked fine.

Not getting compile errors. Just simply isn't storing the data.

I haven't used this code in a couple years and it's not working. Did iOS7 or cocos2d v3 change how NSCoder is used?

Or am I making a dumb error somewhere?

Thanks in advance.

James Webster
  • 31,873
  • 11
  • 70
  • 114
J C
  • 63
  • 1
  • 7
  • why did you comment [super dealloc]? Not a good idea. Anyway, post the code where you actually encode and decode the GameData object. – CodeSmile Mar 26 '14 at 07:02
  • ARC error with [self dealloc]. "ARC forbids explicit message send of 'dealloc'". Every place I have read implies to just comment it out because it's done automatically, supposedly (clause 7.1.2 of the "Objective-C Automatic Reference Counting (ARC)"). I went ahead and changed GameData.m to not compile via ARC by setting a compiler flag (-fno-objc-arc) and uncommented out the dealloc. Compiles fine now with no ARC errors but still no game data saved/retrieved. I thought what I have posted shows it being encoded and decoded? – J C Mar 26 '14 at 20:39
  • I saw "assign" and assumed MRC. Don't change the clas to non-ARC, that's not the issue. What I want to see is the code where you create an NSKeyedArchiver to archive the object and corresponding unarchive. The problem isn't with the GameData class, though I'd be careful with the extern gGameData. A Singleton would be better, and more appropriate a reference in the root object, ie app delegate or view controller. – CodeSmile Mar 26 '14 at 20:49
  • I figured it out. My high score variable was being set to 0 right before saving. Ugh. Thanks for the help! :) how do I up vote you to get answer credit? – J C Mar 26 '14 at 23:57

0 Answers0