Я пытаюсь выполнить свой первый проект сохранения с помощью NSUserDefaults, и что-то странное происходит.Xcode/iOS NSUserDefaults действующий Strange
В моем делете приложения, прежде чем делать что-либо слишком много, я переключаю пару переменных.
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, это правда! 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;
}
Было бы более конструктивным, если бы вы зарегистрировали возвращаемое значение. Посмотрите на фактические данные! – zaph
Зарегистрировать возвращаемое значение? Не знаете, что вы имеете в виду. < – user1636975
Журнал: [defaults objectForKey: @ "world2"] как: 'NSLog (@"% @ world2 after ", [defaults objectForKey: @" world2 "]);' – zaph