gpt4 book ai didi

ios - Xcode/iOS NSUserDefaults 行为奇怪

转载 作者:行者123 更新时间:2023-11-29 10:38:28 26 4
gpt4 key购买 nike

我正在尝试使用 NSUserDefaults 保存我的第一个项目,但发生了一些奇怪的事情。

在我的应用委托(delegate)中,在做任何事情之前,我会切换几个变量。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if (![defaults objectForKey:@"firstRun"]){
//flag doesnt exist then this IS the first run
self.firstRun = TRUE;

NSLog(@"FIRST RUN");

//store the flag so it exists the next time the app starts
[defaults setObject:[NSDate date] forKey:@"firstRun"];
NSLog(@"%s world2 before",[defaults objectForKey:@"world2"] ? "true" : "false" );
[defaults setBool:false forKey:@"world2"];
NSLog(@"%s world2 after",[defaults objectForKey:@"world2"] ? "true" : "false" );
}else{
//flag does exist so this ISNT the first run
self.firstRun = FALSE;
NSLOG(@"NOT FIRST RUN");
NSLog(@"%s world2",[defaults objectForKey:@"world2"] ? "true" : "false" );

}
[[NSUserDefaults standardUserDefaults] synchronize];

这样做,当我运行应用程序时,这是我在控制台中得到的(第一次运行时)

2014-09-09 22:15:57.794 GameName[30151:907] FIRST RUN
2014-09-09 22:15:57.795 GameName[30151:907] false world2 before
2014-09-09 22:15:57.795 GameName[30151:907] true world2 after

我不明白的是为什么在将其设置为 false 之前,它是 false(当然,好吧)- 但在将其设置为 false 之后,它是 true!嗯?

我也尝试过设置为 true、YES、NO,以及将 NSNumber 与 Bool 一起使用。

我的完整应用委托(delegate):

.h

#import <UIKit/UIKit.h>
#import "cocos2d.h"
#import "GameLevelLayer.h"
@interface AppController : CCAppDelegate
{
GameLevelLayer *currentLevel;
}
@property(nonatomic) BOOL firstRun;


@end

.m

#import "cocos2d.h"

#import "AppDelegate.h"
#import "CCBuilderReader.h"


@implementation AppController
@synthesize firstRun = _firstRun;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// Configure Cocos2d with the options set in SpriteBuilder
NSString* configPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Published-iOS"]; // TODO: add support for Published-Android support
configPath = [configPath stringByAppendingPathComponent:@"configCocos2d.plist"];

NSMutableDictionary* cocos2dSetup = [NSMutableDictionary dictionaryWithContentsOfFile:configPath];

// Note: this needs to happen before configureCCFileUtils is called, because we need apportable to correctly setup the screen scale factor.
#ifdef APPORTABLE
if([cocos2dSetup[CCSetupScreenMode] isEqual:CCScreenModeFixed])
[UIScreen mainScreen].currentMode = [UIScreenMode emulatedMode:UIScreenAspectFitEmulationMode];
else
[UIScreen mainScreen].currentMode = [UIScreenMode emulatedMode:UIScreenScaledAspectFitEmulationMode];
#endif

// Configure CCFileUtils to work with SpriteBuilder
[CCBReader configureCCFileUtils];

// Do any extra configuration of Cocos2d here (the example line changes the pixel format for faster rendering, but with less colors)
//[cocos2dSetup setObject:kEAGLColorFormatRGB565 forKey:CCConfigPixelFormat];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if (![defaults objectForKey:@"firstRun"]){
//flag doesnt exist then this IS the first run
self.firstRun = TRUE;

NSLog(@"FIRST RUN");

//store the flag so it exists the next time the app starts
[defaults setObject:[NSDate date] forKey:@"firstRun"];
NSLog(@"%s world2 before",[defaults objectForKey:@"world2"] ? "true" : "false" );
[defaults setBool:false forKey:@"world2"];
NSLog(@"%s world2 after",[defaults objectForKey:@"world2"] ? "true" : "false" );
}else{
//flag does exist so this ISNT the first run
self.firstRun = FALSE;
NSLog(@"NOT FIRST RUN");
NSLog(@"%s world2",[defaults objectForKey:@"world2"] ? "true" : "false" );

}
[[NSUserDefaults standardUserDefaults] synchronize];

[self setupCocos2dWithOptions:cocos2dSetup];

return YES;
}

最佳答案

看来日志条件不正确。您正在使用“setBool”方法设置值。检索时您使用了“objectForKey”方法。

[defaults objectForKey:@"world2"] ? "true" : "false";

上面的行返回“true”,因为它检查用户默认值是否具有键“world2”的值。所以,将该行更改为

 [defaults boolForKey:@"world2"] ? "true" : "false"

关于ios - Xcode/iOS NSUserDefaults 行为奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25756901/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com