iOS development--Data Persistence OC & (iv) Preferences

Source: Internet
Author: User

Preference settings

First, Brief introduction

Many iOS apps support preferences such as saving usernames, passwords, font size, and more, 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, the font size, whether to log on automatically

Storage location:

Storage format:

Second, code example

1.storyboard

2. Code

1//2//Yyviewcontroller.m3//01-Preference Settings4//5//Created by Apple on 14-6-7.6//Copyright (c) 2014 itcase. All rights reserved.7//89#import"YYViewController.h"10//Preference settings11@interfaceYyviewcontroller ()12/**13* Save Data14*/-(Ibaction) SaveData: (Id) sender;16/**17* Read Data18*/-(Ibaction) ReadData: (Id) sender;2021st@end2223@implementationYyviewcontroller24-(Ibaction) SaveData: (Id) Sender {26//1. Get the Nsuserdefaults objectNsuserdefaults *defaults=[Nsuserdefaults Standarduserdefaults];2829//2 Save data (if no synchronization occurs after setting up the data, the data is automatically saved to the Preferences folder at a future point in time)[Defaults setobject:@"Yangyong"Forkey:@"Name"];[Defaults Setinteger:Forkey:@"Age"];[Defaults setdouble:1.73f Forkey:@"Height"];[Defaults setobject:@"Mans"Forkey:@"Gender"];3435//3. Force data to be saved immediately36[Defaults synchronize];37}38-(Ibaction) ReadData: (Id) Sender {40//1. Get the Nsuserdefaults objectNsuserdefaults *defaults=[Nsuserdefaults Standarduserdefaults];42//Read the saved dataNSString *name=[defaults Objectforkey:@"Name"];NSString *gender=[defaults Objectforkey:@"Gender"];Nsinteger age=[defaults Integerforkey:@ "age" ]; 46 double height=[defaults doubleforkey:@ "height" ];47 // print data 48 NSLog ( Span style= "color: #800000;" >@ "name=%@,gender=%@,age=%d,height=%.1f" ,name,gender,age,height); 49 }50  @end                 

3. Click on save data, read the data button to print as follows

Iii. Additional Information

1. Save your data

    //1. Get the Nsuserdefaults object Nsuserdefaults *defaults=[Nsuserdefaults Standarduserdefaults];//2 saving data [defaults setobject:@"Yangyong"Forkey:@"name23 forkey:@ "age ""; [Defaults setdouble:1.73f forkey:@ "height ""; [Defaults setobject:@ "man  "Forkey:@" gender< Span style= "color: #800000;" > ""; //3. Force data to be saved immediately [defaults synchronize];     

2. Reading data

   //1. Get the Nsuserdefaults object Nsuserdefaults *defaults=[nsuserdefaults Standarduserdefaults]; //2. Read saved data nsstring *name=[defaults objectforkey:< Span style= "color: #800000;" >@ "name" gender< Span style= "color: #800000;" > ""; Nsinteger age=[defaults integerforkey:age ""; double height=[defaults doubleforkey:@ "height           

3. Important Notes

(1) Preferences are specifically used to save configuration information for an application, and generally do not save other data in preferences. If you use System Preferences to store data, the default is to store it under the Preferences folder, and the preferences will save all the data in the same file.

(2) After saving the data by using the preferences, the time it saves to the system is indeterminate, the data will be saved to the Preferences folder automatically at a certain point in the future, and if the data is stored immediately, you can use [defaults synchronize];

(3) Note: All the information is written in a single file, compared to simple plist can save and read the basic data types.

(4) Step: Get Nsuserdefaults, Save (read) data

iOS development--Data Persistence OC & (iv) 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.