IOS nsuserdefaults Tutorial Usage
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:
[CPP]View Plaincopy
- #import <UIKit/UIKit.h>
- @interface Viewcontroller:uiviewcontroller
- {
- Iboutlet UILabel *txtinteger;
- Iboutlet UILabel *txtfloat;
- Iboutlet UILabel *txtdouble;
- Iboutlet UILabel *txtnsstring;
- Iboutlet UILabel *txtnsdate;
- Iboutlet UILabel *txtnsarray;
- Iboutlet UILabel *txtnsdictionary;
- }
- @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.
[CPP]View Plaincopy
- #import "ViewController.h"
- @interface Viewcontroller ()
- @end
- @implementation Viewcontroller
- -(void) Viewdidload
- {
- [Super Viewdidload];
- [Self savensuserdefaults]; //Call this method to store various data in NSUSERDEFAUTLS, defined below
- [Self readnsuserdefaults]; //Call this method to read various data from the NSUSERDEFAUTLS, defined below
- }
- -(void) Viewdidunload
- {
- [Txtnsstring release];
- txtnsstring = 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];
- //Release any retained subviews of the main view.
- }
- -(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation
- {
- return (interfaceorientation! = Uiinterfaceorientationportraitupsidedown);
- }
- -(void) Dealloc {
- [Txtnsstring release];
- [Txtnsdate release];
- [Txtnsarray release];
- [Txtnsdictionary release];
- [Txtinteger release];
- [Txtfloat release];
- [Txtdouble release];
- [Super Dealloc];
- }
- Save data to Nsuserdefaults
- -(void) Savensuserdefaults
- {
- NSString *mystring = @"Enuola";
- int myinteger = 100;
- float myfloat = 50.0f;
- double mydouble = 20.0;
- NSDate *mydate = [NSDate Date];
- Nsarray *myarray = [Nsarray arraywithobjects:@"Hello", @"world", nil];
- Nsdictionary *mydictionary = [nsdictionary dictionarywithobjects:[nsarray arraywithobjects:@"Enuo", @"20", Nil] Forkeys:[nsarray arraywithobjects:@"name", @"age", nil]];
- //Store All of the above data in Nsuserdefaults
- Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];
- //storage, except for the NSNumber type using the corresponding type unexpectedly, the others are using Setobject:forkey:
- [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"];
- //This is recommended to be stored synchronously on disk, but is not required
- [Userdefaults Synchronize];
- }
- Reading data from the Nsuserdefaults
- -(void) Readnsuserdefaults
- {
- nsuserdefaults *userdefaultes = [Nsuserdefaults standarduserdefaults];
- //Read the data into each label
- //Read integer int type data
- Nsinteger Myinteger = [userdefaultes integerforkey:@"Myinteger"];
- Txtinteger.text = [NSString stringwithformat:@"%d", Myinteger];
- //Read data for float type float
- float myfloat = [userdefaultes floatforkey:@"Myfloat"];
- Txtfloat.text = [NSString stringwithformat:@"%f", myfloat];
- //Read double type of data
- double mydouble = [userdefaultes doubleforkey:@"MyDouble"];
- Txtdouble.text = [NSString stringwithformat:@"%f", MyDouble];
- //Read nsstring type of data
- NSString *mystring = [userdefaultes stringforkey:@"myString"];
- Txtnsstring.text = myString;
- //Read data of NSDate date type
- NSDate *mydate = [userdefaultes valueforkey:@"MyDate"];
- NSDateFormatter *DF = [[NSDateFormatter alloc] init];
- [DF setdateformat:@"Yyyy-mm-dd HH:mm:ss"];
- Txtnsdate.text = [NSString stringwithformat:@"%@", [DF Stringfromdate:mydate]];
- //Read array nsarray type of data
- Nsarray *myarray = [userdefaultes arrayforkey:@"MyArray"];
- NSString *myarraystring = [[NSString alloc] init];
- For (NSString *str in MyArray)
- {
- NSLog (@"str=%@", str);
- myarraystring = [NSString stringwithformat:@"%@%@", myarraystring, str];
- [Myarraystring STRINGBYAPPENDINGSTRING:STR];
- [Myarraystring stringbyappendingformat:@ "%@", str];
- NSLog (@"myarraystring=%@", myarraystring);
- }
- Txtnsarray.text = myarraystring;
- //Read the dictionary type Nsdictionary data type
- Nsdictionary *mydictionary = [userdefaultes dictionaryforkey:@"MyDictionary"];
- NSString *mydicstring = [NSString stringwithformat:@"name:%@, age:%d", [mydictionary valueforkey:@"name"], [[ MyDictionary valueforkey:@"age"] integervalue];
- Txtnsdictionary.text = mydicstring;
- }
- @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 [self savensuserdefaults]; 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
IOS nsuserdefaults Tutorial Usage