Data Persistence for iPhone application development

Source: Internet
Author: User

IPhone applicationsDevelopmentDataPersistence is the content to be introduced in this article, mainly to learnIphone applicationsMediumDataLibrary usage. For details, see the details.

1. plist

Limitation: only the data types supported by this tool can be serialized and stored in plist. Other Cocoa objects cannot be stored in plist, or custom object storage.

Supported Data types: Array, Dictionary, Boolean, Data, Date, Number, and String.

Xml file data type ~ Where the basic Data is Boolean, Data, Date, Number, and String.), container Array, Dictionary)

Xml writing process: first write the basic data to the container and then call the writeToFile method of the container to write the data.

 
 
  1. [theArray writeToFile:filePath atomically:YES]; 

The data types that have this method are ,:

The atomically parameter. Set the value to YES. When a file is written, data is not directly written to the specified path. Instead, the data is written to a "secondary File". After the file is successfully written, the data is copied to the specified path.

2. Archiver

Feature: supports complex data objects. Includes custom objects. Archive the custom object. The attributes of the object must meet the following requirements: int or float or ......), or an instance that implements the NSCoding protocol class. NSCoding is also required for the class of the custom object.

NSCoding method:

 
 
  1. -id)initWithCoder:(NSCoder *)decoder; - (void)encodeWithCoder:(NSCoder *)encoder;  

The parameters are interpreted as decoded and encoded.

For example, create a custom class Student: NSObject <NSCoding>

 
 
  1. #import "Student.h"   
  2. @implementation Student   
  3. @synthesize studentID;   
  4. @synthesize studentName;   
  5. @synthesize age;   
  6. @synthesize count;   
  7. - (void)encodeWithCoder:(NSCoder *)encoder  
  8. {  
  9.    [encoder encodeObject: studentID forKey: kStudentId];  
  10.     [encoder encodeObject: studentName forKey: kStudentName];  
  11.     [encoder encodeObject: age forKey: kAge];  
  12.      [encoder encodeInt:count forKey:kCount];   
  13. }18 19  - (id)initWithCoder:(NSCoder *)decoder  
  14. {  
  15.      if (self == [super init]) {  
  16.        self.studentID = [decoder decodeObjectForKey:kStudentId];  
  17.        self.studentName = [decoder decodeObjectForKey:kStudentName];          
  18.    self.age = [decoder decodeObjectForKey:kAge];  
  19.         self.count = [decoder decodeIntForKey:kCount];   
  20.     }  
  21.     return self;  
  22. }  
  23. @end 

Encoding Process:

 
 
  1. /* Encoding */
  2. Student * theStudent = [[Student alloc] init];
  3. TheStudent. studentID = @ "Shenma ";
  4. TheStudent. studentName = @ "shenma ";
  5. TheStudent. age = @ "12 ";
  6. NSMutableData * data = [NSMutableData alloc] init];
  7. NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data];
  8. [Archiver encodeObject: theStudent forKey: @ "student"];

NSKeyedArchiver can be considered as a "encryptor". The student instance is encoded and stored in data

NSMutableData can be viewed as a "Container" for writing files (inherits NSData ).

Decoding process:

 
 
  1.  /*unencoding*/  
  2. Student *studento = [[Student alloc] init];  
  3. data = [[NSData alloc] initWithContentsOfFile:documentsPath];  
  4.  NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];  
  5.  studento = [unarchiver decodeObjectForKey:@"student"];  
  6.  [unarchiver finishDecoding]; 

Get the deserialized instance based on the key value.

3. SQLite

Database Operations ~

Summary: DetailsIPhone applicationsDevelopmentDataAfter the introduction of the persistent content, I hope this article will help you!

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.