Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if NSUserDefaults exist

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:

  1. 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

  2. Where do i find it on my Mac (running simulator)? Would need to delete it to test if the first run works?

like image 755
PeterK Avatar asked Jan 23 '11 15:01

PeterK


People also ask

What is NSUserDefaults?

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.

Is NSUserDefaults encrypted?

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 .

Is NSUserDefaults thread safe?

The NSUserDefaults class is thread-safe.

How do I use UserDefaults in swift 5?

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.


1 Answers

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.

like image 95
Matthias Bauch Avatar answered Oct 21 '22 04:10

Matthias Bauch