IOS document serialization (Object archiving)

Source: Internet
Author: User
Tags object serialization

Object Archiving:

Concept:

Object archiving refers to writing objects to a file and saving them on the hard disk. When you re-open the program, you can restore these objects. It is also called object serialization and Object persistence.


Data persistence (in fact, there are three types)

1. NSKeyedArchiver -- Object Archiving

2, NSUserDefaults 3, attribute list

4. SQLite database 5, Core Data database


Archive method:

Archiving objects in the Foundation Library

Custom objects for archiving (archive Protocol is required, NSCoding)


Differences between archive and attribute list:

1. the archived file is encrypted and the attribute list is in plaintext.

2. The property list cannot store custom object types, and archive can.



Code implementation:

// Object archive, object archive int main (int argc, char * argv []) {@ autoreleasepool {// return UIApplicationMain (argc, argv, nil, NSStringFromClass ([AppDelegate class]); // archive (serialization) NSArray * array = @ [@ "abc", @ "123", @ 1234]; NSString * homePath = NSHomeDirectory (); NSLog (@ "homePath: % @", homePath); NSString * path = [homePath stringByAppendingPathComponent: @ "test. archive "]; // obtain BOOL success = [NSKeyedArchiver archiveRootObject: array toFile: path]; if (success) {NSLog (@" archive success ");}} return 0 ;}


This generates an encrypted file.

Open

6270 6c69 7374 3030 d401 0203 0405 081c1d54 2474 6f70 5824 6f62 6a65 6374 73582476 6572 7369 6f6e 5924 6172 6368 69766572 d106 0754 726f 6f74 8001 a609 0a121314 1555 246e 756c 6cd2 0b0c 0d0e 5624636c 6173 735a 4e53 2e6f 626a 6563 74738005 a30f 1011 8002 8003 8004 5361 62635331 3233 1104 d2d2 1617 181b 5824 636c6173 7365 735a 2463 6c61 7373 6e61 6d65a219 1a57 4e53 4172 7261 7958 4e53 4f626a65 6374 574e 5341 7272 6179 1200 0186a05f 100f 4e53 4b65 7965 6441 7263 68697665 7208 1116 1f28 3235 3a3c 4349 4e556062 6668 6a6c 7074 777c 8590 939b a4acb100 0000 0000 0001 0100 0000 0000 00001e00 0000 0000 0000 0000 0000 0000 0000


Int main (int argc, char * argv []) {@ autoreleasepool {// return UIApplicationMain (argc, argv, nil, NSStringFromClass ([AppDelegate class]); // archive (serialization) // NSArray * array = @ [@ "abc", @ "123", @ 1234]; // NSString * homePath = NSHomeDirectory (); // NSLog (@ "homePath: % @", homePath); // NSString * path = [homePath stringByAppendingPathComponent: @ "test. archive "]; // obtain the file and extension at Will // BOOL success = [NSKeyedArchiver archiveRootObject: array toFile: path]; // if (success) {// NSLog (@ "archive success"); //} // unarchive (deserialization) NSString * homePath = NSHomeDirectory (); NSString * path = [homePath stringByAppendingPathComponent: @ "test. archive "]; NSArray * array = [NSKeyedUnarchiver unarchiveObjectWithFile: path]; NSLog (@" NSKeyedUnarchiver: % @ ", array);} return 0 ;}


Defects of this method:

An object is archived into an object, which is very troublesome when many objects exist.

The following method can solve this problem:

Archive:

1. Use an NSData instance as the archive storage data

2. Add archive content (set key and value)

3. Complete Archiving

4. Save the archive data to the disk.


Archive:

1. Read files from the disk and generate an NSData instance

2. Create and initialize an archive instance based on the Data instance

3. archive. Access the value of value based on the key.



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.