Usage of nsuserults ults in ios

Source: Internet
Author: User

Usage of nsuserults ults in ios

Usage of nsuserults ults in ios

 

 

The NSUserDefaults class provides a programming interface for interacting with the default system. The NSUserDefaults object is used to save and restore preference settings and configuration data related to the application. By default, the system allows applications to customize their behaviors to suit users' preferences. You can read the program settings from the user's default database when the program is running. At the same time, the NSUserDefaults cache avoids enabling the user's default database every time the data is read. You can call the synchronize method to synchronize the cache in the memory with the user's default system.

The NSUserDefaults class provides a very convenient way to obtain common types, such as floats, doubles, intergers, Booleans, and URLs. Therefore, an NSUserDefaults object must be an Attribute Table, which means we can store instances such as NSData, NSString, NSNUmber, NSDate, NSArray, and NSDictionary. If you want to store other types of objects, You need to archive them and create an NSData for storage.

The value returned from NSUserDefaults cannot be changed, even if you are using a variable value during storage. For example, if you use mutable string as the value of "MyStringDefault", the value obtained by using the stringForKey: method is still unchangeable.

NSUserDefaults is a singleton and thread-safe

When using NSUserDefaults,

Let's take a look at the following code.

NSDictionary * defaults = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];

NSLog (@ Defaults: % @, defaults );

Is used to get all nsuserults ults settings on the device.

 

NSUserDefaults is suitable for storing lightweight local data, such as the user name and password of a login interface. In my opinion, NSUserDefaults is the first choice. You can directly read the last login information from NSUserDefaults at the next login.

If you use a self-built plist file or something, you have to display the created file and read the file. It is very troublesome. Instead, you can use NSUserDefaults to read the file, just like reading a string, you can simply read it. NSUserDefaults is easy to read.

 

There are multiple methods for creating a user ults, which is the simplest and fastest way to create a user defaults:

 

[Plain] 
  1. NSUserDefaults * accountDefaults = [NSUserDefaults standardUserDefaults];

    Add data to user defaults:

     

    [Plain]
    1. [AccountDefaults setObject: nameField. text forKey: UserDefaultNameKey];

      You can also add basic data types such as int, float, and bool.

       

       

      [Plain]
      1. [AccountDefaults setBool: YES forKey: userdefaboboolkey];

        Get data from user defaults:

         

         

        [Plain]
        1. [AccountDefaults objectForKey: NCUserDefaultNameKey] [accountDefaults boolForKey: UserDefaultBoolKey];

          Key points:

          Nsuserults ults is very easy to use. You do not need to set the global variable of NSUserDefaults in the program. Where to use NSUserDefaults data, create an NSUserDefaults object and perform read or write operations.

          You can rewrite the object or data corresponding to the same keyword. After rewriting, the keyword corresponds to a new object or data, and the old object or data is automatically cleared.


           

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.