Usage of nsuserdefaults in iOS

Source: Internet
Author: User

Usage of nsuserdefaults in iOS


nsuserdefaults class provides a programmatic interface to interact with the default system. The Nsuserdefaults object is used to save, restore application-related preferences, configuration data, and so on . The default system allows the application to customize its behavior to suit the user's preferences. you can read the program's settings from the user's default database while the program is running. At the same time, the nsuserdefaults cache avoids the operation of opening the user's default database every time the data is read. You can synchronize the in-memory cache with the user's default system by calling the Synchronize method.

The Nsuserdefaults class provides a very convenient way to get common types, such as Floats,doubles,intergers,booleans,urls. So a Nsuserdefaults object must be an attribute table, which means we can store nsdata,nsstring,nsnumber,nsdate,nsarray,nsdictionary these instances. If you want to store other types of objects, you need to archive them and create a nsdata to implement the storage.

The value returned from Nsuserdefaults is immutable, even if you are storing it with a variable value. For example, you use the mutable string as the value of "Mystringdefault", and when you do use the Stringforkey: method to get the value, the value is still immutable.

Nsuserdefaults is a singleton, but also a thread-safe

When using the Nsuserdefaults,

First look at the following code

nsdictionary* defaults = [[Nsuserdefaults standarduserdefaults] dictionaryrepresentation];

NSLog (@ "Defaults:%@", Defaults);

is used to get all the devices on the The settings for the nsuserdefaults.


Nsuserdefaults is suitable for storing lightweight local data , such as the data to save a login interface, user name, password, and so on, individuals feel that using nsuserdefaults is preferred. The next time you log in, you can read the last login information directly from the Nsuserdefaults.

Because if you use the Plist file you created, you also have to display the creation of files, read files, very troublesome, but with nsuserdefaults do not care about these things, like reading a string, directly read it. Nsuserdefaults is very convenient and easy to read.


There are several ways to create a user defaults method, the simplest way to create it quickly:

[Plain]View Plaincopy
    1. Nsuserdefaults *accountdefaults = [Nsuserdefaults standarduserdefaults];

Add data to User defaults:

[Plain]View Plaincopy
    1. [Accountdefaults SetObject:nameField.text Forkey:userdefaultnamekey];

You can also add the basic data type int, float, bool, etc., have the corresponding method

[Plain]View Plaincopy
    1. [Accountdefaults Setbool:yes Forkey:userdefaultboolkey];

from get data in user defaults:

[Plain]View Plaincopy
    1. [Accountdefaults Objectforkey:ncuserdefaultnamekey]
    2. [Accountdefaults Boolforkey:userdefaultboolkey];

Points:

Nsuserdefaults is very useful and does not require the user to set up in the program nsuserdefaults Global variables, where to use nsuserdefaults data, then where to create a The Nsuserdefaults object is then read or written.

For the same keyword corresponding to the object or data, it can be rewritten, after rewriting the keyword corresponding to the new object or data, the old object or data will be automatically cleaned up.



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.