OC Data Persistence scheme 1-nsuserdefauts and properties file (plist)

Source: Internet
Author: User

Nsuserdefauts

User defaults, this is a class that Apple has provided to us to easily read the plist properties file, so what is plist?


About the Plist file

Plist file, also known as a property file, is essentially an XML file that can be used to store arrays, dictionaries, strings, numbers, etc., many times, many of the app's configuration items are also implemented through the Plist file.

We open a plist file to see:


There is a key-value pair, of course, you can also save the array, it will need to make a lot of item


Reading the value using the Nsuserdefaults store is actually writing the read data to the plist file in the appropriate sandbox:

In fact, we can use it in the simplest way, that is, using object objects to store the form, regardless of any data type, I eventually converted to object storage,


1. Use Nsuserdefauts to store data (fixed process):

Nsuserdefaults *user = [Nsuserdefaults standarduserdefaults];    NSString *key = [NSString stringwithformat:@ "%d", Self.count];    NSString *value = [NSString stringwithformat:@ "store value%d", self.count];//the value to be stored, can eventually be converted to an object to save   [user Setobject:value forkey:key];//writes the data and overwrites the previous value if the previous key already exists   [user synchronize];//writes the file immediately

2. Get Data

   Nsuserdefaults *user = [Nsuserdefaults standarduserdefaults];    NSString *key = [NSString stringwithformat:@ "%d", Self.count];    NSString *value = [user objectforkey:key];//Gets the value you stored based on key    NSLog (@ "read data is%@", value);
3. Removing data

    [user removeobjectforkey:key];//removes data from key    [user synchronize];//Save, delete operation is immediately synced to file


Summary: Whether it is a dictionary, arrays, numbers, BOOL, and other types, we can use the door of a attract storage, that is, first converted to objects, and then storage!


More OC Articles: Http://blog.csdn.net/yangbingbinga





OC Data Persistence scheme 1-nsuserdefauts and properties file (plist)

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.