Usage of Nsuserdefaults in iOS (lightweight local data store) (RPM)

Source: Internet
Author: User
Tags uikit

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.

The data formats supported by Nsuserdefaults are: NSNumber (Integer, Float, Double), Nsstring,nsdate,nsarray,nsdictionary,bool type. It's practical.

Nsuserdefaults is very convenient and easy to read. Here is an example to see how to use: (PS: More details can also refer to the official document HA)

The main ViewController.h file is to put a few controls to display the stored data:

1 #import <UIKit/UIKit.h> 2  3 @interface viewcontroller:uiviewcontroller 4 {5      6     iboutlet UILabel *txt Integer; 7     iboutlet UILabel *txtfloat; 8     iboutlet UILabel *txtdouble; 9     iboutlet UILabel *txtnsstring;10     Iboutlet UILabel *txtnsdate;11     iboutlet UILabel *txtnsarray;12     iboutlet UILabel *txtnsdictionary;13}14 @end

The most important of the VIEWCONTROLLER.M files are two methods:

Savensuserdefaults: Used to save various types of data to Nsuserdefaults

READNSUSERDEFAUTLS: Used to read various types of data from the Nsuserdefaults. Call these two methods in Viewdidload to see the results.

  1 #import "ViewController.h" 2 3 @interface Viewcontroller () 4 5 @end 6 7 @implementation Viewcontroller 8  9-(void) viewdidload [Super Viewdidload]; [Self savensuserdefaults];  Call this method to store various data in NSUSERDEFAUTLS, defined below [self readnsuserdefaults]; Call this method to read a variety of data from the NSUSERDEFAUTLS, defined below: (void) viewdidunload-{[txtnsstring release]; Txtnsstrin g = nil; [Txtnsdate release]; Txtnsdate = nil; [Txtnsarray release]; Txtnsarray = nil; [Txtnsdictionary release]; Txtnsdictionary = nil; [Txtinteger release]; Txtinteger = nil; [Txtfloat release]; Txtfloat = nil; [Txtdouble release]; Txtdouble = nil; [Super Viewdidunload]; Retained/Release Any subviews of the main view.  -(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation (Interfaceorientation! = UiinterfaceorientatIonportraitupsidedown);     (void) Dealloc {[txtnsstring release]; [Txtnsdate release]; [Txtnsarray release]; 46 [Txtnsdictionary release]; [Txtinteger release]; [Txtfloat release]; [Txtdouble release]; [Super Dealloc]; 51} 52 53//Save data to Nsuserdefaults-(void) savensuserdefaults nsstring *mystring = @ "Enuola"; Teger = 100; myfloat float = 50.0f; Double mydouble = 20.0; NSDate *mydate = [NSDate Date]; Nsarray *myarray = [Nsarray arraywithobjects:@ "Hello", @ "World", nil]; Nsdictionary *mydictionary = [nsdictionary dictionarywithobjects:[nsarray arraywithobjects:@ "Enuo", @ "+", nil] for Keys:[nsarray arraywithobjects:@ "name", @ "age", nil]]; 63 64//store all the above data in nsuserdefaults nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults]; 66//storage, except for NSNumber type use the corresponding type unexpectedly, others are using setobject:forkey:67 [Userdefaults setinteger:myinteger Forkey: @ "Myinteger"]; [Userdefaults setfloat:myfloat forkey:@ "Myfloat"]; [Userdefaults setdouble:mydouble forkey:@ "MyDouble"]; [Userdefaults setobject:mystring forkey:@ "myString"]; [Userdefaults setobject:mydate forkey:@ "MyDate"]; [Userdefaults setobject:myarray forkey:@ "MyArray"]; [Userdefaults setobject:mydictionary forkey:@ "MyDictionary"]; 75 76//This is recommended to be stored synchronously to disk, but is not necessary for the [userdefaults Synchronize]; 78 79} 80 81//Read data from Nsuserdefaults-(void) readnsuserdefaults nsuserdefaults *userdefaultes = [Nsuse Rdefaults Standarduserdefaults]; 85 86//Read data into each label 87//Read data of integer type int Nsinteger myinteger = [Userdefaultes integerforkey:@ "Myinteger "]; Txtinteger.text = [NSString stringwithformat:@ "%d", Myinteger]; 90 91//Read data for float type float myfloat = [userdefaultes floatforkey:@ "Myfloat"]; Txtfloat.text = [NSString stringwithformat:@ "%f", myfloat]; 94 95//Read DoubleType of data in a double mydouble = [Userdefaultes doubleforkey:@ "MyDouble"]; Txtdouble.text = [NSString stringwithformat:@ "%f", MyDouble]; 98 99//Read NSString type of data nsstring *mystring = [userdefaultes stringforkey:@ "myString"];101 txtnsstring. Text = mystring;102 103//Read data of NSDate date type 104 NSDate *mydate = [userdefaultes valueforkey:@ "MyDate"];105 NS Dateformatter *DF = [[NSDateFormatter alloc] init];106 [DF setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];107 Txtnsdate.tex t = [NSString stringwithformat:@ "%@", [DF stringfromdate:mydate]];108 109//Read array nsarray type data Nsarray *myarra y = [userdefaultes arrayforkey:@ "MyArray"];111 nsstring *myarraystring = [[NSString alloc] init];112 for (nsstring *str in MyArray) 113 {NSLog (@ "str=%@", str); myarraystring = [NSString stringwithformat:@]%@%@ ", Myarraystring, str];116 [myarraystring stringbyappendingstring:str];117//[myarraystring StringByAppend Ingformat:@ "%@", str];118 NSLog (@ "myarraystring=%@", myarraystring); 119}120 Txtnsarray.text = myarraystring;121 122//Read the data of the dictionary type Nsdictionary type 123 nsdictionary *mydictionary = [userdefaultes dictionaryforkey:@ "MyDictionary"] ; 124 NSString *mydicstring = [NSString stringwithformat:@ "name:%@, age:%d", [mydictionary valueforkey:@ "name"], [[MyDic Tionary valueforkey:@ "Age"] integervalue]];125 Txtnsdictionary.text = mydicstring;126}127 @end

OK, run it, can you see the various data in the Xib file is already tied up?

Run again, you can put viewdidload in the [selfsavensuserdefaults]; This line is commented out, allowing the program to read directly without storing the data, discovering that the previously saved data can still be read to the interface.

Hehe, very simple, so it can be the implementation of data storage.

Here's how it works:

You may ask a question: Where does the NSUSERDEFAUTLS store the data??? I have not shown the specified path??? Very confused ....

Data stored with Nsuserdefaults still exists the next time the program runs, where does it store the data? How can I clear?

In fact it is stored in a plist file built into the application, which can be seen according to the path. For example, this is your program sandbox location.
/userslibrary/application support/iphonesimulator/4.1/applicati*****/29788e40-af47-45a0-8e92-3ac0f501b7f4/, (This is where the application corresponds on the Mac)
There is a/library/prefereces, there is a plist file, stored is your userdefaults
If you want to erase it, use Removeobjectforkey or delete the sandbox, which is your application and then reinstall it.

Usage of Nsuserdefaults in iOS (lightweight local data store) (RPM)

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.