我正在尝试使用NSUserDefaults进行我的第一个保存项目,并且发生了一些奇怪的事情。
在我的app委托中,在做任何事情之前,我正在切换几个变量。
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
我不明白为什么在将其设置为假之前,它是错误的(当然,没关系) - 但是在将其设置为假之后,这是真的! MERR?
我也试过设置为true,YES,NO,以及将NSNumber与Bool一起使用。
我的完整应用代表:
·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;
}
答案 0 :(得分:1)
您需要使用:
- (BOOL)boolForKey:(NSString *)defaultName
不
- (id)objectForKey:(NSString *)defaultName
后者返回一个对象,表示为false,因此NSLog()
中的测试报告为true,表示已返回某些内容。
所以代码是:
[defaults boolForKey:@"world2"]
答案 1 :(得分:1)
似乎日志条件不正确。您正在使用“setBool”方法设置值。在检索时使用了“objectForKey”方法。
[defaults objectForKey:@"world2"] ? "true" : "false";
上面的行返回“true”,因为它检查用户默认值是否具有键“world2”的值。所以,将该行更改为
[defaults boolForKey:@"world2"] ? "true" : "false"