Home IT Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ iOS persistence of data archiving NSKeyedArchiver     - Linux NFS FTP use (Server)

- MySQL Basic tutorial: About varchar (N) (Database)

- What have we learn from the front-end application Nodejs (Programming)

- Linux system security settings (Linux)

- Linux Timing task Crontab command Detailed (Linux)

- WinSCP to transfer text files will automatically convert the format (Linux)

- Select helps secure the system network management tools (Linux)

- Why did not Oracle privileges can also log in with sysdba (Database)

- Give your photos Instagram style filters plus program in ubuntu (Linux)

- Linux System Getting Started Learning: install software packages on Ubuntu and Fedora (Linux)

- CentOS 6.x and CentOS7 install MPlayer (Linux)

- Let MySQL 5.6 support Emoji expression (Database)

- Windows using SSH management Ubuntu (Linux)

- Repair Chrome for Linux is (Linux)

- Graphics of Java Tools (Programming)

- Linux process management related content (Linux)

- Python configuration tortuous road of third-party libraries Numpy and matplotlib (Programming)

- Spring Boot + Nginx + Tomcat + SSL configuration notes (Server)

- Tomcat installation under Linux (Server)

- Acquaintance C ++: the most simple C ++ program (Programming)

  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
- ImageMagick Tutorial: How to cut images in Linux command line (Linux)
- BusyBox build root file system (Linux)
- Python configuration tortuous road of third-party libraries Numpy and matplotlib (Programming)
- Install Firefox 28 on Ubuntu, Linux Mint (Linux)
- [JavaScript] catch (ex) statements of ex (Programming)
- Configuring DNS process under CentOS 6.5 (Server)
- Fedora 22 how to play rmvb mp4 mkv video files (Linux)
- Setting CentOS firewall open port (Linux)
- Bash added to the Vi mode indicator (Linux)
- Ubuntu install Liferea news subscription software (Linux)
- Android 4.2 compilation notes (Programming)
- ARM assembler instruction debugging method (Programming)
- Bash Automated Customization Linux belongs to its own CentOS system (Linux)
- Ubuntu Tutorial: How to Upgrade a New Linux Kernel 3.12.7 on Ubuntu (Linux)
- FreeBSD install Gnome 3 Desktop (Linux)
- Hibernate Search and Lucene quick introduction (Linux)
- Android will save the picture to see the album and timely (Programming)
- Oracle lag () and lpad () function (Database)
- Android project using the command to create and install the package (Programming)
- Git Installation and Configuration (Network Agent settings) (Linux)
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.