Attribute list of iOS data persistent storage, ios Data Storage

Source: Internet
Author: User

Attribute list of iOS data persistent storage, ios Data Storage
Property list (plist)

IOS provides a plist format file (attribute list) for storing lightweight data. It can only store data of the NSDictionary, NSArray, NSString, NSNumber, Boolean, NSData, and NSDate types. Save these types of data as plist files. The stored data can be directly read using NSDictionary and NSArray.

(1) Use NSUserDefault for persistence

Next, let's take a look at the NSUserDefault local storage location, which is mentioned in the sandbox directory of data persistence. The plist file under the Library/Preferences directory is its saved directory.
NSUserDefault is used to store and read data.

Initialize an NSUserDefault

    + (NSUserDefaults *)standardUserDefaults;

How to Set Data

    - (void)setObject:(nullable id)value forKey:(NSString *)defaultName;    - (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName;    - (void)setFloat:(float)value forKey:(NSString *)defaultName;    - (void)setDouble:(double)value forKey:(NSString *)defaultName;    - (void)setBool:(BOOL)value forKey:(NSString *)defaultName;    - (void)setURL:(nullable NSURL *)url forKey:(NSString *)defaultName NS_AVAILABLE(10_6, 4_0);

Data Reading method:

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

How to delete data:

    - (void)removeObjectForKey:(NSString *)defaultName;

Save data:

// If you do not call it manually, the system will automatically save it, but the time is not fixed

   - (BOOL)synchronize;
 

// Store id data

+ (Void) setValue :( id) value andKey :( NSString *) key

{

NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];

[UserDefaults setObject: value forKey: key];

[UserDefaults synchronize];

}

    // Obtain data

+ (NSString *) getValueByKey :( NSString *) key

{

NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];

NSString * value = [settings objectForKey: key];

Return value;

}

 

(2) manually add a plist File

Create a file --> Resource --> Property List

The root type of plist files can only be NSArray or NSDictionary.

    

 

Read the data in the plist file into the corresponding root type

// 1. Obtain the path of the file. Resource: file name and Type: File Format

NSString * filePath = [[NSBundle mainBundle] pathForResource: @ "userInfos" ofType: @ "plist"];

// 2. Obtain the data in the corresponding format from the path

// If the Root value is NSArray, save it using an array

NSArray * infos = [NSArray arrayWithContentsOfFile: filePath];

NSLog (@ "% @", infos );

// If Root is NSDictionary, it is saved in the dictionary.

NSDictionary * infoDic = [NSDictionary dictionaryWithContentsOfFile: filePath];

NSLog (@ "% @", infoDic );

 

(3) writing data directly to the plist File

NSUserDefault is essentially a plist file to realize property persistence. Therefore, we can create a plist file to realize property persistence.

NSArray * path = require (NSDocumentDirectory, NSUserDomainMask, YES); NSString * docPath = [path objectAtIndex: 0]; NSString * myFile = [docPath stringByAppendingPathComponent: @ "test. plist "]; NSMutableDictionary * contentDic; // determine whether the local plist file exists if ([[NSFileManager defaultManager] fileExistsAtPath: myFile] = NO) {NSFileManager * fm = [NSFileManager defaultManager]; // create a file [fm createFileAtPath: myFile contents: nil attributes: nil]; contentDic = [[NSMutableDictionary alloc] init];} else {contentDic = [[NSMutableDictionary alloc] initWithContentsOfFile: myFile];} // data read/write operations [contentDic setObject: @ "1234" forKey: @ "passWord"]; // Save the modified data to the plist file [contentDic writeToFile: myFile atomically: YES];

(4) Summary
Plist files are highly efficient in reading and writing. Since they need to retrieve and store all the data, it is only suitable for small data.

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.