What is the use of - [NSUserDefaults registerDefaults:]?

What's the difference between:

[[NSUserDefaults standardUserDefaults] registerDefaults: [NSDictionary dictionaryWithObjectAndKey:anObject, @"something"]]; 

And this:

 [[NSUserDefaults standardUserDefaults] setObject:anObject forKey:@"something"]; 
+64
iphone cocoa-touch nsuserdefaults
Feb 08 2018-11-11T00:
source share
5 answers

The difference is that the first code fragment you register defaults that will be used when the user has not made any changes to the "property".

So, if you want to let let say say “property” with the key name “Welcome message”, you can instead of returning the nil property, insert the default message “Welcome first-time user”, which will be displayed when it has not been changed into the property.

This will simplify your logic because you do not need to write an if test to check if the property returns the nil property and then creates another message if that is the case.

 NSString *greeting = [[NSUserDefaults standardUserDefaults] stringForKey:@"Greeting"]; if(greeting == nil) { NSLog(@"Welcome first-time user!"); } 

The second piece of code you submitted is for setting the property to a different value. You will have different set methods (setString, setObject, setBoolean) for setting values ​​depending on your program state in Userdefaults.

EDIT ----- Updated on request in the comments.

The first method is designed to register default values, as the name suggests. When you first access a property with some key name, the value will be either zero for objects, or false for boolean values, or 0 for numbers. Instead of doing a lot of tests and so on, so that if the values ​​are not set in the program, and then do something "by default", for example, the example above, you can send your application with some predefined values ​​for these keys.

A typical place to set registerDefaults is in the initializer method in appDelegate.

Then somewhere in your program you can set the values ​​of these fields, then you use setObject, setString, setBoolean ... and for extraction you use stringForKey, objectForKey ...

Think of it as

RegisterDefaults is a constructor where you can provide reasonable values ​​for an object, otherwise you will get some default values ​​that I already wrote. Then, if you want to change the attributes of an object, you are NOT using the "constructor", but the set / get methods.

+81
Feb 08 2018-11-11T00:
source share
— -

In short,

 [[NSUserDefaults standardUserDefaults] setObject:@"Entropy" forKey:@"kName"] 

saves "Entropy" to a file named com.example.Demo.plist in the Library / Preference folder (where com.example.Demo is your package identifier, see IOS Application Protection Part 20 - Local data storage )

 [[NSUserDefaults standardUserDefaults] setObject:@"Mac OS X" forKey:@"kOS"]; NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys: @"Windows", @"kOS", @"Google", @"kSearchEngine", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults]; NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"kOS"]); NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"kSearchEngine"]); NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"kBrowser"]); 

will print Mac OS X, Google, (null)

Indeed registerDefaults

  • not saved to disk
  • sets the value for undefined keys ("kOS" is setObject:forKey: and "kSearchEngine" is not set)
  • returns 0 for scalar values, nil for objects if this key is not set for both registerDefaults and setObject:forKey: (in this case "kBrowser")

And using registerDefaults

Quote from the Settings Guide

If these standard defaults are not suitable for your application, you can register your own default values ​​using the registerDefaults: method. This method puts your custom defaults in an NSRegistrationDomain, which causes them to return when the preference is not explicitly set.

Quote from How to save data using NSUserDefaults

One more tip: you can initialize NSUserDefaults using a predefined NSDictionary object. So, for example, you can set the default value to “false” or “true” before the user has ever had the opportunity to interact with your program. In my case, sometimes I create an array that represents all the levels in my game, and in each value array I store a boolean to check if the player has completed the level. To do this, I create a data object and then register it with NSUserDefaults. If the previous value exists for the object, then Nothing happened. Otherwise, my empty object will be saved as "by default", by default

PS: Ole has a very good article explaining a detailed description of the default values ​​with NSUserDefaults

+30
Nov 07 '13 at 13:21
source share

Another way to look at this. If you delete the preferences file from ~ / Library / Preferences, the default values ​​set by registerDefaults will be applied to the application until new settings are set.

+10
Feb 09 '11 at 11:15
source share

In Swift 2.1.1 Xcode 7.2

I added this snippet to the application: didFinishLaunchingWithOptions to initialize tintColorsIndex, which is one of the parameters that the user can change in the application.

  let defaults = NSUserDefaults.standardUserDefaults() defaults.registerDefaults([ "tintColorsIndex" : -1, ]) 

When the application is launched for the first time, tintColorsIndex will be set to -1 (Int). If the user changes color when using the application, their preferences will not be canceled on subsequent launches.

+3
Mar 13 '16 at 3:35
source share

User defaults are grouped in domains ... registerDefaults is used to add default values ​​to the registration domain.

You can read about domains in the Settings and Settings Programming Guide .

+1
Feb 08 2018-11-11T00:
source share



All Articles