IOS data persistence file read/write preference settings, ios data read/write preferences

Source: Internet
Author: User

IOS data persistence file read/write preference settings, ios data read/write preferences

Many iOS apps support preference settings, such as saving user names, passwords, and font sizes. iOS provides a standard solution to add preference settings to apps. Each application has an NSUserDefaults instance to access preference settings. For example, save the username, font size, and whether to log on automatically. NSUserDefaults supports all native data types such as NSString, NSNumber, NSDate, NSArray, NSDictionary, BOOL, NSInteger, and NSFloat.

1. Introduction to preference settings

You should remember that there is a folder named Preferences in the sandbox directory of the application written to the Library folder in the reading and writing of iOSDay35 data processing files ), used to save the preference settings of the application. When you set preference, a plist file with the same project name will be automatically created in this folder, and the preference settings will be stored in this plist file.

Since it is stored in a plist file, it naturally exists as a key-value pair. Its storage form is actually an XML file:

2. Use preference settings 1> save data
1 // create an NSUserDefaults object to set preference settings 2 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; 3 4 // set preference 5 [defaults setObject: @ "zf" forKey: @ "name"]; 6 [defaults setInteger: 24 forKey: @ "age"]; 7 [defaults setBool: YES forKey: @ "gender"]; 8 9 // synchronize 10 [defaults synchronize] immediately;

After saving the data with preference settings, the time it takes to save the data to the system is uncertain, and the data will be automatically saved to the Preferences folder at a certain time point in the future, if you need to store data immediately, you must use defaults synchronize

The NSUserDefaults class encapsulates a series of set methods to save different types of data.

1 - (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName;2 - (void)setFloat:(float)value forKey:(NSString *)defaultName;3 - (void)setDouble:(double)value forKey:(NSString *)defaultName;4 - (void)setBool:(BOOL)value forKey:(NSString *)defaultName;5 - (void)setURL:(nullable NSURL *)url forKey:(NSString *)defaultName NS_AVAILABLE(10_6, 4_0);
2> Read data
1 // read data 2 NSString * name = [defaults objectForKey: @ "name"]; 3 int age = (int) [defaults integerForKey: @ "age"]; 4 BOOL gender = [defaults boolForKey: @ "gender"];

Print results

Similar to data storage, the NSUserDefaults class encapsulates a series of data obtaining methods.

 1 - (nullable NSString *)stringForKey:(NSString *)defaultName; 2 - (nullable NSArray *)arrayForKey:(NSString *)defaultName; 3 - (nullable NSDictionary<NSString *, id> *)dictionaryForKey:(NSString *)defaultName; 4 - (nullable NSData *)dataForKey:(NSString *)defaultName; 5 - (nullable NSArray<NSString *> *)stringArrayForKey:(NSString *)defaultName; 6 - (NSInteger)integerForKey:(NSString *)defaultName; 7 - (float)floatForKey:(NSString *)defaultName; 8 - (double)doubleForKey:(NSString *)defaultName; 9 - (BOOL)boolForKey:(NSString *)defaultName;10 - (nullable NSURL *)URLForKey:(NSString *)defaultName NS_AVAILABLE(10_6, 4_0);

3> delete data

1 // Delete preference settings 2 [defaults removeObjectForKey: @ "name"]; 3 [defaults removeObjectForKey: @ "age"]; 4 [defaults removeObjectForKey: @ "gender"];

Note: preference settings are used to save application configuration information. Generally, do not save other data in preference settings. If you use the preference settings of the system to store data, the Preferences folder is stored by default. The Preferences folder stores all data in the same file.

Similar to NSDictionary in the basic data type, you can rewrite the object or data corresponding to the same keyword. After rewriting, the keyword corresponds to the new object or data, old objects or data are automatically cleared.

 

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.