Usage of NSUserDefaults in IOS (lightweight local data storage)

Source: Internet
Author: User

NSUserDefaults is suitable for storing lightweight local data, such as the user name and password of a login interface. In my opinion, NSUserDefaults is the first choice. You can directly read the last login information from NSUserDefaults at the next login.

If you use a self-built plist file or something, you have to display the created file and read the file. It is very troublesome. Instead, you can use NSUserDefaults to read the file, just like reading a string, you can simply read it.

NSUserDefaults supports the NSNumber (Integer, Float, Double), NSString, NSDate, NSArray, NSDictionary, and BOOL data formats. Very practical

NSUserDefaults is easy to read. The following example shows how to use IT: (PS: For more details, refer to the official documentation)

The ViewController. h file mainly contains several controls for displaying stored data:


[Cpp]
# 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
Two methods are most important in the ViewController. m file:

SaveNSUserDefaults: used to save various types of data to NSUserDefaults.


ReadNSUserDefautls: used to read various types of data from NSUserDefaults. You can see the result by calling these two methods in viewDidLoad.


[Cpp]
# Import "ViewController. h"
 
@ Interface ViewController ()
 
@ End
 
@ Implementation ViewController
 
-(Void) viewDidLoad
{
[Super viewDidLoad];
 
[Self saveNSUserDefaults]; // call this method to store various data in NSUserDefautls, which is defined below
[Self readNSUserDefaults]; // call this method to read various data from NSUserDefautls, which is 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 the data to NSUserDefaults
-(Void) saveNSUserDefaults
{
NSString * myString = @ "enuola ";
Int myInteger = 100;
Float myFloat = 50366f;
Double myDouble = 20.0;
NSDate * myDate = [NSDate date];
NSArray * myArray = [NSArray arrayWithObjects: @ "hello", @ "world", nil];
NSDictionary * myDictionary = [NSDictionary Syntax: [NSArray arrayWithObjects: @ "enuo", @ "20", nil] forKeys: [NSArray arrayWithObjects: @ "name", @ "age ", nil];

// Store all the above data in NSUserDefaults
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
// During storage, except for the NSNumber type, the corresponding type is used unexpectedly. Other operations use 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"];

// We recommend that you synchronize the data to the disk, but it is not required.
[UserDefaults synchronize];

}
 
// Read data from NSUserDefaults
-(Void) readNSUserDefaults
{
NSUserDefaults * userDefaultes = [NSUserDefaults standardUserDefaults];

// Read data to each label
// Read data of the int type.
NSInteger myInteger = [userDefaultes integerForKey: @ "myInteger"];
TxtInteger. text = [NSString stringWithFormat: @ "% d", myInteger];

// Read float data
Float myFloat = [userDefaultes floatForKey: @ "myFloat"];
TxtFloat. text = [NSString stringWithFormat: @ "% f", myFloat];

// Read data of the double Type
Double myDouble = [userDefaultes doubleForKey: @ "myDouble"];
TxtDouble. text = [NSString stringWithFormat: @ "% f", myDouble];

// Read NSString data
NSString * myString = [userDefaultes stringForKey: @ "myString"];
TxtNSString. text = myString;

// Read data of the 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 the data of the array NSArray type
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 data of the dictionary type NSDictionary type
NSDictionary * myDictionary = [userDefaultes dictionaryForKey: @ "myDictionary"];
NSString * myDicString = [NSString stringWithFormat: @ "name: % @, age: % d", [myDictionary valueForKey: @ "name"], [[myDictionary valueForKey: @ "age"] integerValue];
TxtNSDictionary. text = myDicString;
}
 
@ End


 

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.