I have been looking fo开发者_StackOverflowr an answer but not really found what i am looking for.
I have an application and is using NSUserDefaults to store 'currentGameStatus' and would like to ask the following questions:
How do i check if the NSUserDefaults .plist exists? Need this to determine if i need to create it for the first time and if so fill it with default values
Where do i find it on my Mac (running simulator)? Would need to delete it to test if the first run works?
you don't check.
you register your defaults. and if you haven't saved a value the default will be used.
NSDictionary *defaultUserDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], @"Foo",
@"Bar", @"Baz",
[NSNumber numberWithInteger:12], @"FooBar",
nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultUserDefaults];
and you do this every time your app launches.
The way I do it is I set a BOOL
flag in NSUserDefaults
if it doesn't already exist:
if(![[NSUserDefaults standardUserDefaults] boolForKey:@"firstRun"]) {
//do initialization stuff here...
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstRun"];
}
NSUserDefaults already exists by default. You can add to it by
[[NSUserDefaults standardUserDefaults] setObject:@"object" forKey:@"key"];
You can find the NSUserDefaults .plist here
精彩评论