麻烦在单身IOS中设置长值

时间:2015-02-27 19:26:14

标签: ios singleton long-integer

我在设置和访问Singleton对象上的长属性时遇到问题。每次我尝试访问一个长属性时,它返回-1。

我的单例界面文件如下:

 @interface gameData : NSObject <NSCoding>
    @property (assign, nonatomic) long score;
    @property (assign, nonatomic) long level;
    @property (assign, nonatomic) long riddlesCompleted;
    @property (assign, nonatomic) long hints;
    @property (assign, nonatomic) long firstLetters;
    @property (assign, nonatomic) long answers;

    +(instancetype)sharedGameData;
    -(void)reset;
    -(void)save;

  @end

然后,实现文件按如下方式设置编码器和解码器:

-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeDouble:self.score forKey:gameDataScoreKey];
    [aCoder encodeDouble:self.level forKey:gameDataLevelKey];
    [aCoder encodeDouble:self.riddlesCompleted forKey:gameDataRiddlesCompletedKey];
    [aCoder encodeDouble:self.hints forKey:gameDataHintsKey];
    [aCoder encodeDouble:self.firstLetters forKey:gameDataFirstLettersKey];
    [aCoder encodeDouble:self.answers forKey:gameDataAnswersKey];
}
-(instancetype)initWithCoder:(NSCoder *)decoder{
    self = [self init];
    if (self) {
        _score = [decoder decodeDoubleForKey:gameDataScoreKey];
        _level = [decoder decodeDoubleForKey:gameDataLevelKey];
        _riddlesCompleted = [decoder decodeDoubleForKey:gameDataRiddlesCompletedKey];
        _hints = [decoder decodeDoubleForKey:gameDataHintsKey];
        _firstLetters = [decoder decodeDoubleForKey:gameDataFirstLettersKey];
        _answers = [decoder decodeDoubleForKey:gameDataAnswersKey];
    }
    return self;
}

+(instancetype) sharedGameData{
    static id sharedInstance = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [self loadInstance];
    });
    return sharedInstance;
}

值初始化如下:

-(id)init{
    if(self = [super init]){
        _score = 500;
        _riddlesCompleted = 0;
        _level = 1;
        _hints = 3;
        _firstLetters = 3;
        _answers = 3;
    }
    return self;
}

然后加载实例:

+(NSString*)filePath{
    static NSString* filePath = nil;
    if (!filePath) {
        filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) firstObject]
                    stringByAppendingString:@"gameData"];
    }
    return filePath;
}

+(instancetype)loadInstance{
    NSData* decodeData = [NSData dataWithContentsOfFile:[gameData filePath]];
    if (decodeData) {
        gameData* gameData = [NSKeyedUnarchiver unarchiveObjectWithData:decodeData];
        return gameData;
    }
    return [[gameData alloc] init];
}

然后当我尝试访问这些值时应用程序中的其他位置,我无法访问提示,首字母或答案的值。

如果我尝试按如下方式记录值:

NSLog([NSString stringWithFormat:@"%li", [gameData sharedGameData].score] );
NSLog([NSString stringWithFormat:@"%li", [gameData sharedGameData].hint] );
NSLog([NSString stringWithFormat:@"%li", [gameData sharedGameData].answers]);
NSLog([NSString stringWithFormat:@"%li", [gameData sharedGameData].firstLetters]);

我获得的输出为500分,但对于所有其他我得到0,即使它们在gameData.m文件中初始化为值为3.

1 个答案:

答案 0 :(得分:1)

猜猜: 在loadInstance()中,您从文件中读取值,因此如果有文件,则不会进入设置值的init()。

为确保没有gamedata文件,您应重置模拟器或从设备中删除该应用程序,然后重试。