I have been looking for 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?
The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an app to customize its behavior to match a user's preferences. For example, you can allow users to specify their preferred units of measurement or media playback speed.
NSUserDefaults is easy to incorporate into your application and unfortunately, that means it is frequently misused by developers of all skill levels. Because NSUserDefaults stores all data in an unencrypted .
The NSUserDefaults class is thread-safe.
Writing or Setting Values To User Defaults You simply invoke the set(_:forKey:) method on the UserDefaults instance. In this example, we set the value of myKey to true by invoking the set(_:forKey:) method on the shared defaults object.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With