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.
- [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:
- -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>
- #import "Student.h"
- @implementation Student
- @synthesize studentID;
- @synthesize studentName;
- @synthesize age;
- @synthesize count;
- - (void)encodeWithCoder:(NSCoder *)encoder
- {
- [encoder encodeObject: studentID forKey: kStudentId];
- [encoder encodeObject: studentName forKey: kStudentName];
- [encoder encodeObject: age forKey: kAge];
- [encoder encodeInt:count forKey:kCount];
- }18 19 - (id)initWithCoder:(NSCoder *)decoder
- {
- if (self == [super init]) {
- self.studentID = [decoder decodeObjectForKey:kStudentId];
- self.studentName = [decoder decodeObjectForKey:kStudentName];
- self.age = [decoder decodeObjectForKey:kAge];
- self.count = [decoder decodeIntForKey:kCount];
- }
- return self;
- }
- @end
Encoding Process:
- /* Encoding */
- Student * theStudent = [[Student alloc] init];
- TheStudent. studentID = @ "Shenma ";
- TheStudent. studentName = @ "shenma ";
- TheStudent. age = @ "12 ";
- NSMutableData * data = [NSMutableData alloc] init];
- NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data];
- [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:
- /*unencoding*/
- Student *studento = [[Student alloc] init];
- data = [[NSData alloc] initWithContentsOfFile:documentsPath];
- NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
- studento = [unarchiver decodeObjectForKey:@"student"];
- [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!