iOS Data Persistence archive Nskeyedarchiver

Source: Internet
Author: User
Tags list of attributes

There are three ways to persist iOS data:

  1. List of properties (custom property list, Nsuserdefaults)
  2. Archive (Nskeyedarchiver)
  3. Database (SQLite, Core Data, third-party class libraries, etc.)

The following is a main introduction to an archive nskeyedarchiver.

Archive (aka serialization), convert objects to bytecode, save them to disk as files, and restore them by unpacking (deserializing) the program as it runs or when it is rewritten again.

How to archive:

  • Archiving objects in the foundation framework
  • Archive your Customizations
  • Archive your custom objects
< > Archiving of objects in the foundation framework
Get file path    nsstring *documentpath = [Nssearchpathfordirectoriesindomains (NSDocumentDirectory, NSUserDomainMask, YES) Lastobject];    NSString *filepath = [documentpath stringbyappendingpathcomponent:@ "File.archiver"];    Archive (serialization) Nsarray *archiveary = @[@ "Jereh", @ "ios"];if ([Nskeyedarchiver archiverootobject:archiveary Tofile:filepath] {        NSLog (@ "Archiver  success");}    Solution Archive (deserialization) Nsarray *unarchiveary = [Nskeyedunarchiver Unarchiveobjectwithfile:filepath]; NSLog (@ "%@", unarchiveary);

Summary:

  1. Easy archiving and archiving procedures
  2. Only one object can be archived at a time, if multiple object archives need to be separated
  3. Archived objects are objects in the foundation framework
  4. Archive and archive any of these objects need to be archived and archived throughout the file.
  5. Archived files are encrypted, so the file extension is free to take
< two > archiving of customized Content
Get file path NSString *documentpath = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, NSUserDomainMask, YES) Lastobject];    NSString *filepath = [documentpath stringbyappendingpathcomponent:@ "File.archiver"]; 1. Use NSData to store archived data nsmutabledata *archiverdata = [Nsmutabledata data];//2. Create archive object Nskeyedarchiver *archiver = [[Nskeyedarchiver alloc] INITFORWRITINGWITHMUTABLEDATA:ARCHIVERDATA];//3. Add archived content (set key value pairs) [archiver encodeobject:@ "Jereh" forkey:@ "name"]; [Archiver encodeint:20 forkey:@ "age"]; [Archiver encodeobject:@[@ "ios", @ "OC"] forkey:@ "language"];//4. Complete the archive [archiver FINISHENCODING];//5.    Store archived information on disk if ([Archiverdata writetofile:filepath Atomically:yes]) {NSLog (@ "archiver success");} Archive//1. Read files from disk, generate NSData instance nsdata *unarchiverdata = [NSData datawithcontentsoffile:filepath];//2. Create and initialize the archive object based on the data instance nskeyedunarchiver *unachiver = [[Nskeyedunarchiver alloc] initforreadingwithdata:unarchiverdata] ;//3. Archive, access NSString *name = [unachiver decodeobjectforkey:@ "name"];int AG based on key value)e = [Unachiver decodeintforkey:@ "age"]; Nsarray *ary = [Unachiver decodeobjectforkey:@ "language"]; NSLog (@ "name=%@ age=%i ary=%@", name,age,ary);

Summary:

  1. In a keyed archive, each archive field has a key value that matches the key value when it is archived
  2. With key archive You can store multiple objects at once
  3. Archived objects are objects in the foundation framework
  4. Archive and archive any of these objects need to be archived and archived throughout the file.
  5. Archived files are encrypted, so the file extension is free to take
< three > archiving of Custom Objects
#define IDNUM @ "Idnum" #define NAME @ "name" @interface student:nsobject <NSCoding> @property (nonatomic, assign) int Idnum; @property (nonatomic, copy) NSString *name; @end @implementation Student#pragma Mark encodes the processing of object properties-(void) Encodewithcoder: (Nscoder *) acoder{    [Acoder encodeint:_idnum forkey:idnum];    [Acoder encodeobject:_name forkey:name];}  #pragma mark decodes and decodes archived data to initialize the object-(ID) Initwithcoder: (Nscoder *) adecoder{    if (self = [super init]) {        _idnum = [Adecoder Decodeintforkey:idnum];        _name = [Adecoder decodeobjectforkey:name];    }    return self;} @end

Summary:

  1. Custom objects are identical to custom content archiving and archiving steps and usage
  2. Custom object archiving requires implementing the Nscoding protocol and implementing the Methods in the Protocol
  3. There are two methods in the Nscoding protocol:
    • The Encodewithcoder method encodes an object property, which is called when an object is archived
    • The Initwithcoder method decodes the archived data to initialize the object, which is called when the object is archived

Summarize:

    1. Archive and archive can be used for persistent storage and reading of small amounts of data
    2. The list of attributes can only store objects in the foundation framework, and archives can archive custom objects that implement the Nscoding protocol in addition to objects in the foundation framework
    3. Files created by archive are encrypted

iOS Data Persistence archive Nskeyedarchiver

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.