Use of NSUserDefault and NSUserDefault

Source: Internet
Author: User

Use of NSUserDefault and NSUserDefault
Use of NSUserDefault

NSUserDefaults is used to store small data such as user configurations.

1. Create an NSUserDefaults object

NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];

 

 

2. After creating the NSUserDefaults object, you can add data to it. The supported data types include NSString, NSNumber, NSDate, NSArray, NSDictionary, BOOL, NSInteger, NSFloat, and other system-defined data types, if you want to store custom objects (such as custom class objects), you must convert them to NSData storage:

NSArray * arr = [[NSArray alloc] initWithObjects: @ "arr1", @ "arr2", nil]

[UserDefault setObject: arr forKey: @ "arrItem"];

[UserDefault setObject: @ "admin" forKey: @ "user_name"];

[UserDefault setBOOL: @ YES forKey: @ "auto_login"];

[UserDefault setInteger: 1 forKey: @ "count"];

 

 

3. After adding data to NSUserDefaults, they become global variables, and the data in NSUserDefault can be read and written in the App:

NSUserDefaults * mySettingDataR = [NSUserDefaults standardUserDefaults];

 

NSLog (@ "arrItem = % @", [mySettingDataR objectForKey: @ "arrItem"]);

NSLog (@ "user_name = % @", [mySettingDataR objectForKey: @ "user_name"]);

NSLog (@ "count = % d", [mySettingDataR integerForKey: @ "count"]);

 

 

4. If you want to delete a data item, you can use removeObjectForKey to delete the data:

[MySettingData removeObjectForKey: @ "arrItem"];

 

 

5. Note that NSUserDefaults regularly writes data in the cache to the disk, instead of writing data instantly. To prevent data loss caused by program exit after NSUserDefaults is written, you can use synchronize to force data to be written to the disk immediately after data is written:

[MySettingData synchronize];

 

 

6. If a custom class is written, the following two methods should be implemented in the class:

-(Id) initWithCoder: (NSCoder *) coder

{

If (self = [super init])

{

// Your code

Self. _ firstName = [coder decodeObjectForKey: @ "_ firstName"];

Self. _ lastName = [coder decodeObjectForKey: @ "_ lastName"];

}

Return self;

}

 

-(Void) encodeWithCoder: (NSCoder *) coder

{

// Your code

[Coder encodeObject: _ firstName forKey: @ "_ firstName"];

[Coder encodeObject: _ lastName forKey: @ "_ lastName"];

}

 

 

7. Use NSData as the carrier when accessing the database:

BusinessCard * bc = [[BusinessCard alloc] init];

NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];

// Convert the object to NSData

NSData * udObject = [NSKeyedArchiver archivedDataWithRootObject: bc];

[Ud setObject: udObject forKey: @ "myBusinessCard"];

 

UdObject = nil;

UdObject = [ud objectForKey: @ "myBusinessCard"];

// Converts NSData to an object

Bc = [NSKeyedUnarchiver unarchiveObjectWithData: udObject];

Related Article

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.