IOS nsuserdefaults Tutorial Usage

Source: Internet
Author: User

 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
  1. #import <UIKit/UIKit.h>
  2. @interface Viewcontroller:uiviewcontroller
  3. {
  4. Iboutlet UILabel *txtinteger;
  5. Iboutlet UILabel *txtfloat;
  6. Iboutlet UILabel *txtdouble;
  7. Iboutlet UILabel *txtnsstring;
  8. Iboutlet UILabel *txtnsdate;
  9. Iboutlet UILabel *txtnsarray;
  10. Iboutlet UILabel *txtnsdictionary;
  11. }
  12. @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
  1. #import "ViewController.h"
  2. @interface Viewcontroller ()
  3. @end
  4. @implementation Viewcontroller
  5. -(void) Viewdidload
  6. {
  7. [Super Viewdidload];
  8. [Self savensuserdefaults]; //Call this method to store various data in NSUSERDEFAUTLS, defined below
  9. [Self readnsuserdefaults]; //Call this method to read various data from the NSUSERDEFAUTLS, defined below
  10. }
  11. -(void) Viewdidunload
  12. {
  13. [Txtnsstring release];
  14. txtnsstring = nil;
  15. [Txtnsdate release];
  16. Txtnsdate = nil;
  17. [Txtnsarray release];
  18. Txtnsarray = nil;
  19. [Txtnsdictionary release];
  20. Txtnsdictionary = nil;
  21. [Txtinteger release];
  22. Txtinteger = nil;
  23. [Txtfloat release];
  24. Txtfloat = nil;
  25. [Txtdouble release];
  26. Txtdouble = nil;
  27. [Super Viewdidunload];
  28. //Release any retained subviews of the main view.
  29. }
  30. -(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation
  31. {
  32. return (interfaceorientation! = Uiinterfaceorientationportraitupsidedown);
  33. }
  34. -(void) Dealloc {
  35. [Txtnsstring release];
  36. [Txtnsdate release];
  37. [Txtnsarray release];
  38. [Txtnsdictionary release];
  39. [Txtinteger release];
  40. [Txtfloat release];
  41. [Txtdouble release];
  42. [Super Dealloc];
  43. }
  44. Save data to Nsuserdefaults
  45. -(void) Savensuserdefaults
  46. {
  47. NSString *mystring = @"Enuola";
  48. int myinteger = 100;
  49. float myfloat = 50.0f;
  50. double mydouble = 20.0;
  51. NSDate *mydate = [NSDate Date];
  52. Nsarray *myarray = [Nsarray arraywithobjects:@"Hello", @"world", nil];
  53. Nsdictionary *mydictionary = [nsdictionary dictionarywithobjects:[nsarray arraywithobjects:@"Enuo", @"20",  Nil] Forkeys:[nsarray arraywithobjects:@"name", @"age", nil]];
  54. //Store All of the above data in Nsuserdefaults
  55. Nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];
  56. //storage, except for the NSNumber type using the corresponding type unexpectedly, the others are using Setobject:forkey:
  57. [Userdefaults setinteger:myinteger forkey:@"Myinteger"];
  58. [Userdefaults setfloat:myfloat forkey:@"Myfloat"];
  59. [Userdefaults setdouble:mydouble forkey:@"MyDouble"];
  60. [Userdefaults setobject:mystring forkey:@"myString"];
  61. [Userdefaults setobject:mydate forkey:@"MyDate"];
  62. [Userdefaults setobject:myarray forkey:@"MyArray"];
  63. [Userdefaults setobject:mydictionary forkey:@"MyDictionary"];
  64. //This is recommended to be stored synchronously on disk, but is not required
  65. [Userdefaults Synchronize];
  66. }
  67. Reading data from the Nsuserdefaults
  68. -(void) Readnsuserdefaults
  69. {
  70. nsuserdefaults *userdefaultes = [Nsuserdefaults standarduserdefaults];
  71. //Read the data into each label
  72. //Read integer int type data
  73. Nsinteger Myinteger = [userdefaultes integerforkey:@"Myinteger"];
  74. Txtinteger.text = [NSString stringwithformat:@"%d", Myinteger];
  75. //Read data for float type float
  76. float myfloat = [userdefaultes floatforkey:@"Myfloat"];
  77. Txtfloat.text = [NSString stringwithformat:@"%f", myfloat];
  78. //Read double type of data
  79. double mydouble = [userdefaultes doubleforkey:@"MyDouble"];
  80. Txtdouble.text = [NSString stringwithformat:@"%f", MyDouble];
  81. //Read nsstring type of data
  82. NSString *mystring = [userdefaultes stringforkey:@"myString"];
  83. Txtnsstring.text = myString;
  84. //Read data of NSDate date type
  85. NSDate *mydate = [userdefaultes valueforkey:@"MyDate"];
  86. NSDateFormatter *DF = [[NSDateFormatter alloc] init];
  87. [DF setdateformat:@"Yyyy-mm-dd HH:mm:ss"];
  88. Txtnsdate.text = [NSString stringwithformat:@"%@", [DF Stringfromdate:mydate]];
  89. //Read array nsarray type of data
  90. Nsarray *myarray = [userdefaultes arrayforkey:@"MyArray"];
  91. NSString *myarraystring = [[NSString alloc] init];
  92. For (NSString *str in MyArray)
  93. {
  94. NSLog (@"str=%@", str);
  95. myarraystring = [NSString stringwithformat:@"%@%@", myarraystring, str];
  96. [Myarraystring STRINGBYAPPENDINGSTRING:STR];
  97. [Myarraystring stringbyappendingformat:@ "%@", str];
  98. NSLog (@"myarraystring=%@", myarraystring);
  99. }
  100. Txtnsarray.text = myarraystring;
  101. //Read the dictionary type Nsdictionary data type
  102. Nsdictionary *mydictionary = [userdefaultes dictionaryforkey:@"MyDictionary"];
  103. NSString *mydicstring = [NSString stringwithformat:@"name:%@, age:%d", [mydictionary valueforkey:@"name"], [[  MyDictionary valueforkey:@"age"] integervalue];
  104. Txtnsdictionary.text = mydicstring;
  105. }
  106. @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

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.