Use of Nsuserdefaults Standarduserdefaults

Source: Internet
Author: User

There are three simple ways to store data locally: Databases, Nsuserdefaults, and files.

Nsuserdefaults is used to store data with small amounts of data, such as User Configuration. Not all things can be put in, only support: Nsstring,nsnumber, NSDate, Nsarray, Nsdictionary, detailed methods can view the class file.

Nsuserdefaultsstandarduserdefaults is used to record the data of permanent retention is very convenient, do not need to read and write files, but retained in a nsdictionary dictionary, saved to the file by the system, the system will be saved to the application The Library/preferences/gongcheng.plist file. It is important to note that if the program exits unexpectedly, the nsuserdefaultsstandarduserdefaults data is not written to the file by the system, but can be used [[Nsuserdefaultsstandarduserdefaults] Synchronize] command is synced directly to the file to avoid data loss.

First, store the data in Nsuserdefaults:

Uiswitch

-(Ibaction) switchchanged: (ID) sender{

Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];

[Userdefaults setbool:_theswitch.on forkey:@ "Switchvalue"];

}

Uitextfield

-(Ibaction) inputchanged: (ID) sender{

Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];

[Userdefaults setobject:_textfield.text forkey:@ "Inputvalue"];

}

Second, read the data in the Nsuserdefaults:

Uiswitchnsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults]; BOOL SW = [userdefaults boolforkey:@ "Switchvalue"]; [_theswitch seton:sw];//uitextfieldnsstring *str = [userdefaults stringforkey:@ "Inputvalue"]; [_textfield SETTEXT:STR];
Registerdefaults: Method is a subset of the registration preferences, it is not written to the plist file, but in the ND can be taken indeed.
This means that the data you see in the Plist file is the settings you have shown.
Call the Setxxx method, for example

Use of Nsuserdefaults Standarduserdefaults

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.