Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ iOS persistence of data archiving NSKeyedArchiver     - Deep understanding of C # generics (Programming)

- RAID configuration and management under linux (Server)

- FPM quickly create packages with RPM (Linux)

- Red Hat Linux mount U disk (Linux)

- Ubuntu simple method to track multiple time zones (Linux)

- Create your own YUM repository (Linux)

- Object-oriented language Java some of the basic features (Programming)

- Linux System Getting Started Tutorial: Linux file permissions brief description (Linux)

- In-depth summary of the PHP core of object-oriented (Programming)

- sqlplus connect to Oracle (Database)

- The REVERSE function of DB2 (Database)

- How to force Linux users to change the initial password the first time you log in (Linux)

- Linux, security encryption to transfer files between machines (Linux)

- PostgreSQL Stream Configuration (Database)

- Killall five options you might not know (Linux)

- Ubuntu in Vim editor display processing method Chinese garbled (Linux)

- Node.js Getting the basics: Helloworld! (Linux)

- Create, modify, delete users, user groups under linux (Linux)

- Construction Spark source and application development environment (Server)

- Use MD5 transform algorithm to prevent exhaustive decipher passwords (Linux)

  iOS persistence of data archiving NSKeyedArchiver
  Add Date : 2017-04-19      
  IOS data persistence is divided into three types:

Property list (Property List custom, NSUSErDefaults)
Archive (NSKeyedArchiver)
Database (SQLite, Core Data, third-party libraries, etc.)
Here to introduce a major archive NSKeyedArchiver.

 Archive (aka serialization), the object is converted to byte code in the form of a file stored on disk; or when the program is running again rewrite the open program, you can restore them by solving archiving (deserialization) object.


Foundation Framework for archiving objects
The contents of the custom archive
Custom object archive

< A> Foundation framework for archiving objects

// Get the 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");
// Solutions for Archiving (deserialization)
NSArray * unArchiveAry = [NSKeyedUnarchiver unarchiveObjectWithFile: filePath];
NSLog (@ "% @", unArchiveAry);

Archiving Archiving Reconciliation simple steps
You can only archive an object if it is an archive multiple objects need to be separated
Objects are archived in the Foundation framework objects
Archive unarchive any object which need to be archived unarchive the entire file
After the filing is encrypted, so the extension of the archive can be freely taken

< B> for custom content archive

// Get the 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 objects
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: archiverData];
// 3. Add the contents of the archive (set key-value pairs)
 [Archiver encodeObject: @ "jereh" forKey: @ "name"];
 [Archiver encodeInt: 20 forKey: @ "age"];
 [Archiver encodeObject: @ [@ "ios", @ "oc"] forKey: @ "language"];
// 4. Complete archive
 [Archiver finishEncoding];
// 5. The information is stored on disk archive
if ([archiverData writeToFile: filePath atomically: YES]) {
     NSLog (@ "archiver success");
// Archive Solutions
// 1 read from the disk file to generate NSData instance
NSData * unarchiverData = [NSData dataWithContentsOfFile: filePath];
// 2. Create and initialize an instance according to Data Solutions archive objects
NSKeyedUnarchiver * unachiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: unarchiverData];
// 3. Archiving solutions, based on key values ​​access
NSString * name = [unachiver decodeObjectForKey: @ "name"];
int age = [unachiver decodeIntForKey: @ "age"];
NSArray * ary = [unachiver decodeObjectForKey: @ "language"];
NSLog (@ "name =% @ age =% i ary =% @", name, age, ary);

Keyed in the archive, the archive each field has a key value, key value to match the key value archiving solution archives
Keyed Archiving can store multiple objects once
Objects are archived in the Foundation framework objects
Archive unarchive any object which need to be archived unarchive the entire file
After the filing is encrypted, so the extension of the archive can be freely taken

< C> to target custom archive

#define IDNUM @ "idNum"
#define NAME @ "name"

@interface Student: NSObject < NSCoding>
@property (nonatomic, assign) int idNum;
@property (nonatomic, copy) NSString * name;

@implementation Student

#pragma mark coding process of coding object properties
- (Void) encodeWithCoder: (NSCoder *) aCoder
    [ACoder encodeInt: _idNum forKey: IDNUM];
    [ACoder encodeObject: _name forKey: NAME];

#pragma mark decoding decodes archive data to initialize the object
- (Id) initWithCoder: (NSCoder *) aDecoder
    if (self = [super init]) {
        _idNum = [aDecoder decodeIntForKey: IDNUM];
        _name = [aDecoder decodeObjectForKey: NAME];
    return self;

Custom objects and custom content archive archive reconciliation steps and use the same
Since the object definition archiving NSCoding need to implement the agreement, and implement the protocol Method
NSCoding protocol has two methods:
encodeWithCoder method to encode object properties, called when the object archive
initWithCoder archive data decoding method to initialize the object is called when the object archiving solution

to sum up:

Archiving can be used to archive reconciliation persistent store small amounts of data and reading
Property lists are only stored Foundation framework objects, in addition to the archive can be archived in the Foundation framework objects can also be archived to achieve a custom object NSCoding agreement
Create archive files are encrypted
- Spring JDBC Comments (Programming)
- JavaScript Advanced Programming notes event capture and event bubbling (Programming)
- Linux host system monitoring implement the cgi (Server)
- CentOS 6.7 install Nagios Tutorials (Server)
- GROUP BY extension (Database)
- Quickly locate the mistakes by gdb location (Programming)
- Use Nginx as a load balancer (Server)
- Monitoring network traffic with Iptraf in Linux environment (Linux)
- Articles do not resolve after opening under Ubuntu WordPress setting a fixed link (Server)
- VMware virtual machine to install CentOS 7 (Linux)
- Network Security Basics Linux command (Linux)
- Use Linux firewall camouflage defense hacked (Linux)
- Installation Sublime Text 3 (Build 3065) text editor in Ubuntu (Linux)
- How to install Perl modules from CPAN (Linux)
- Oracle 10g New Features - Archive Compression (Database)
- Oracle GoldenGate Installation and Configuration Tutorial Introduction (Database)
- Comparison of one-time transaction and CTE insert data (Database)
- After reloading the cinder-volume OpenStack not start properly (Server)
- Distributed File System FastDFS deployment (Server)
- Talking about modern programming language syntax and standard library tightly bound phenomenon (Programming)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.