Ios 变量在xcode中返回意外的0

Ios 变量在xcode中返回意外的0,ios,objective-c,xcode,Ios,Objective C,Xcode,变量“bubbleBankCapacity”预计返回为500,并在下面的比较中与“regularBubbleCount”变量进行比较,但返回为0,这实际上使条件为假,因为regularBubbleCount大于0。当它被设置为初始化为500时,为什么返回0 AppDelegate.m 您需要在方法中初始化AppDelegate.m中的bubbleBankCapacity变量。已解决 我继续向RWGameData声明了一个布尔属性,并指定reset方法在调用reset方法时将其设置为true。然后

变量“bubbleBankCapacity”预计返回为500,并在下面的比较中与“regularBubbleCount”变量进行比较,但返回为0,这实际上使条件为假,因为regularBubbleCount大于0。当它被设置为初始化为500时,为什么返回0

AppDelegate.m
您需要在方法中初始化AppDelegate.m中的bubbleBankCapacity变量。

已解决

我继续向RWGameData声明了一个布尔属性,并指定reset方法在调用reset方法时将其设置为true。然后我继续调整-BOOLapplication:UIApplication*应用程序完成启动时使用了选项:NSDictionary*启动选项,如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    if (![RWGameData sharedGameData].dataIsInitialized) {
        [[RWGameData sharedGameData] reset];
    }

    NSTimer *timer;
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCalled) userInfo:nil repeats:YES];

    return YES;
}

感谢所有为这个问题提供解决方案的人。我非常感激

在共享游戏数据实例的何处调用重置?你为什么认为应该是500英镑?哦,糟了。谢谢你@Paulw11。所以这里真正的问题是,在哪里是初始化这些变量的最佳位置?它们已经在标题中声明,但是当它们第一次被引用时,我需要为它们设置值,也就是程序第一次运行时。我建议您在dispatch_block中调用reset。更好的做法是显式定义一个init方法,并在那里调用reset。如果您看看他是如何构造代码的,dispatch_once将调用loadInstance,它将加载持久化数据或创建一个新实例。请注意,显然initWithCoder正在调用init,但这些值稍后将被解码的值覆盖。
#import <Foundation/Foundation.h>

@interface RWGameData : NSObject <NSCoding>

@property (assign, nonatomic) long regularBubbleCount;
@property (assign, nonatomic) long premiumBubbleCount;

@property (assign, nonatomic) long megaBubbleUpgradeTier;
@property (assign, nonatomic) long bubbleFactoryUpgradeTier;
@property (assign, nonatomic) long bubblersUpgradeTier;
@property (assign, nonatomic) long mysteryBubbleUpgradeTier;
@property (assign, nonatomic) long bubbleBankUpgradeTier;

@property (assign, nonatomic) int megaBubblePopValue;
@property (assign, nonatomic) int bubbleFactoryTickValue;

@property (assign, nonatomic) long bubbleBankCapacity;

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

@end
#import "RWGameData.h"

@implementation RWGameData

static NSString* const SSGameDataRegularBubbleCountKey = @"regularBubbleCount";
static NSString* const SSGameDataPremiumBubbleCountKey = @"premiumBubbleCount";

static NSString* const SSGameDataMegaBubbleUpgradeTierKey = @"megaBubbleUpgradeTier";
static NSString* const SSGameDataBubbleFactoryUpgradeTierKey = @"bubbleFactoryUpgradeTier";
static NSString* const SSGameDataBubblersUpgradeTierKey = @"bubblersUpgradeTier";
static NSString* const SSGameDataMysteryBubbleUpgradeTierKey = @"mysteryBubbleUpgradeTier";
static NSString* const SSGameDataBubbleBankUpgradeTierKey = @"bubbleBankUpgradeTier";

static NSString* const SSGameDataMegaBubblePopValueKey = @"megaBubblePopValueKey";
static NSString* const SSGameDataBubbleFactoryTickValueKey = @"bubbleFactoryTickValueKey";

static NSString* const SSGameDataBubbleBankCapacityKey = @"bubbleBankCapacityKey";

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

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

    return sharedInstance;
}

-(void)reset {
    self.regularBubbleCount = 0;
    self.premiumBubbleCount = 0;

    self.megaBubbleUpgradeTier = 0;
    self.bubbleFactoryUpgradeTier = 0;
    self.bubblersUpgradeTier = 0;
    self.mysteryBubbleUpgradeTier = 0;
    self.bubbleBankUpgradeTier = 0;

    self.megaBubblePopValue = 1;
    self.bubbleFactoryTickValue = 1;

    self.bubbleBankCapacity = 500;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeDouble:self.regularBubbleCount forKey: SSGameDataRegularBubbleCountKey];
    [encoder encodeDouble:self.premiumBubbleCount forKey: SSGameDataPremiumBubbleCountKey];

    [encoder encodeDouble:self.megaBubbleUpgradeTier forKey: SSGameDataMegaBubbleUpgradeTierKey];
    [encoder encodeDouble:self.bubbleFactoryUpgradeTier forKey: SSGameDataBubbleFactoryUpgradeTierKey];
    [encoder encodeDouble:self.bubblersUpgradeTier forKey: SSGameDataBubblersUpgradeTierKey];
    [encoder encodeDouble:self.mysteryBubbleUpgradeTier forKey: SSGameDataMysteryBubbleUpgradeTierKey];
    [encoder encodeDouble:self.bubbleBankUpgradeTier forKey: SSGameDataBubbleBankUpgradeTierKey];

    [encoder encodeDouble:self.megaBubblePopValue forKey: SSGameDataMegaBubblePopValueKey];
    [encoder encodeDouble:self.bubbleFactoryTickValue forKey: SSGameDataBubbleFactoryTickValueKey];

    [encoder encodeDouble:self.bubbleBankCapacity forKey: SSGameDataBubbleBankCapacityKey];
}

- (instancetype)initWithCoder:(NSCoder *)decoder
{
    self = [self init];
    if (self) {
        _regularBubbleCount = [decoder decodeDoubleForKey: SSGameDataRegularBubbleCountKey];
        _premiumBubbleCount = [decoder decodeDoubleForKey: SSGameDataPremiumBubbleCountKey];

        _megaBubbleUpgradeTier = [decoder decodeDoubleForKey: SSGameDataMegaBubbleUpgradeTierKey];
        _bubbleFactoryUpgradeTier = [decoder decodeDoubleForKey: SSGameDataBubbleFactoryUpgradeTierKey];
        _bubblersUpgradeTier = [decoder decodeDoubleForKey: SSGameDataBubblersUpgradeTierKey];
        _mysteryBubbleUpgradeTier = [decoder decodeDoubleForKey: SSGameDataMysteryBubbleUpgradeTierKey];
        _bubbleBankUpgradeTier = [decoder decodeDoubleForKey: SSGameDataBubbleBankUpgradeTierKey];

        _megaBubblePopValue = [decoder decodeDoubleForKey: SSGameDataMegaBubblePopValueKey];
        _bubbleFactoryTickValue = [decoder decodeDoubleForKey: SSGameDataBubbleFactoryTickValueKey];

        _bubbleBankCapacity = [decoder decodeDoubleForKey: SSGameDataBubbleBankCapacityKey];
    }
    return self;
}

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

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

    return [[RWGameData alloc] init];
}

-(void)save
{
    NSData* encodedData = [NSKeyedArchiver archivedDataWithRootObject: self];
    [encodedData writeToFile:[RWGameData filePath] atomically:YES];
}

@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    if (![RWGameData sharedGameData].dataIsInitialized) {
        [[RWGameData sharedGameData] reset];
    }

    NSTimer *timer;
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerCalled) userInfo:nil repeats:YES];

    return YES;
}