iOS document serialization (object archive)

Source: Internet
Author: User
Tags object serialization sqlite database

Object Archive:

Concept:

Object archiving is the ability to write objects to a file on a hard disk, and to restore those objects when the program is opened again. Also known as: Object serialization, Object persistence.


The way data is persisted (in fact, the 3 category)

1,nskeyedarchiver--Object Archive

2,nsuserdefaults 3, Property list

4,sqlite Database 5,core Data database


How to archive:

Archiving objects in the Foundation library

Define your own objects for archiving (need to implement archiving Protocol, nscoding)


Differences between archive and attribute list:

1, the archived file is encrypted, and the attribute list is clear text.

2, the attribute list cannot store its own defined object type, which can be archived.



Code implementation:

Object archive, object Anti-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"];  Files and extensions casually take                BOOL success = [Nskeyedarchiver Archiverootobject:array tofile:path];        if (success) {            NSLog (@ "archive success");        }    }    return 0;}


This is the generation of an encrypted file.

Open it

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"];  File and extension free pick////        BOOL success = [Nskeyedarchiver Archiverootobject:array tofile:path];//        if (success) {            NSLog (@ "archive success");/                /}//Solution archive (deserialization)        nsstring *homepath = Nshomedirectory ();        NSString *path = [HomePath stringbyappendingpathcomponent:@ "test.archive"];        Nsarray *array = [Nskeyedunarchiver Unarchiveobjectwithfile:path];        NSLog (@ "nskeyedunarchiver:%@", array);    }    return 0;}


Flaws in this approach:

An object is archived into a file that is cumbersome when the object is many.

The following methods can solve the problem:

Archive:

1, using the NSData instance as the archived storage data

2. Add archived content (set key and value)

3, Complete archive

4. Storing archived data on disk


Solution Archive:

1. read file from disk, generate NSData instance

2, create and initial dissolve archive instance according to data instance

3, unpack, and access the value of value based on key.



iOS document serialization (object archive)

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.