Preference of data storage for iOS development (preferences)

Source: Internet
Author: User

1 , overview

Many iOS apps support preferences such as saving usernames, passwords, font size, and so on, and iOS offers a standard set of solutions to add preferences to your app.

Each app has a Nsuserdefaults instance that accesses preferences. For example, save the user name, font size, and whether to log in automatically.

nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];

[Defaults setobject: @ "itcast" forkey: @ "username"];

[Defaults setfloat: 18.0f forkey: @ "Text_size"];

[Defaults setbool: YES forkey: @ "Auto_login"];

[Defaults synchronize];

3. Sync now to synchronize in-memory data with preferences, suggest calling

[Defaults synchronize];

2 , read the last saved settings

Nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];

NSString *username = [Defaults stringforkey: @ "username"];

float textSize = [Defaults floatforkey: @ "Text_size"];

BOOL autologin = [Defaults boolforkey: @ "Auto_login"];

Note: When setting the data, Userdefaults does not write immediately, but instead writes the cached data to the local disk according to the timestamp. So after calling the set method, it is possible that the data has not yet been written to the disk application to terminate. The above problem can be forced by calling the Synchornize method to write

[Defaults synchornize];

Preference of data storage for iOS development (preferences)

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.