Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should register your defaults before you try to access a value stored in your NSUserDefaults. You could do it in <code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions</code>.<br> Registering your defaults is fast, so there is no need to optimize this. Just do it at the launch of the app. </p> <p>I store my userdefaults in a plist and register the content of this list, like this:</p> <pre><code>NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"DefaultUserDefaults" ofType:@"plist"]]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; </code></pre> <p>If you register your defaults like this you don't have to worry that you overwrite user supplied values. NSUserdefaults uses "domains" where it stores it's values. If you register your defaults they are stored in the registration domain. If a User stores a value those values are stored in the application domain. </p> <p>If you try to get a value from NSUserdefaults it looks if the value is present in the application domain, and if it's not there it takes the value from the registration domain. </p> <hr> <p>Edit: </p> <p><img src="https://i.stack.imgur.com/C51Lq.png" alt="plist editor"></p> <p>you would access those values (or better, the values that are stored in your nsuserdefaults, and those as a fallback if there are no user provided values) like this:</p> <pre><code>NSInteger minutesToWait = [[NSUserDefaults standardUserDefaults] integerForKey:@"MinutesToWait"; NSString *urlString = [[NSUserDefaults standardUserDefaults] stringForKey:@"DefaultURLStr"]; </code></pre> <p>The plist is just another representation of a NSDictionary with keys and values. The key is the same key you use to access the userdefaults, and the value is your defaultvalue. Pretty straight forward.<br> It doesn't matter how you create the dictionary. You can do it in code as well. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload