Automatic archiving and file transfer of Runtime NSCoding, runtimenscoding

Source: Internet
Author: User

Automatic archiving and file transfer of Runtime NSCoding, runtimenscoding

Why does Runtime NSCoding Support automatic archiving and file transfer?

Well-known methods

// Archive Method

-(Void) encodeWithCoder :( NSCoder *) ACO

{

// When students are archived, students must archive all members.

[Ecoder encodeObject: self. name forKey: @ "name"];

[Ecoder encodeInteger: self. age forKey: @ "age"];

}

// Archive Method

-(Id) initWithCoder :( NSCoder *) aDecoder

{

If (self = [super init]) {

// When a student is archived, the student must archive all the members.

Self. name = [aDecoder decodeObjectForKey: @ "name"];

Self. age = [aDecoder decodeIntegerForKey: @ "age"];

}

Return self;

}

This is because there is a small amount of data. What if there are dozens of records? How do you feel when writing dozens of lines of such code? If one byte is wrong, you do not have this data.

This shows the automatic archiving and file transfer of Runtime.

Runtime has a way to get the attribute list in the current class. Since member variables can be obtained, the key and value corresponding to each member variable can also be obtained, in this way, you do not need to archive and archive one row by one row.

# Import "person. h"

@ Implementation person

// File-based read data

-(Instancetype) initWithCoder :( NSCoder *) aDecoder {

If (self = [super init]) {

/*

OBJC_EXPORT Ivar * class_copyIvarList (Class cls, unsigned int * outCount)

Class cls indicates getting the attribute list of that Class

Unsigned int * outCount is used to store the number of attributes obtained.

*/

Unsigned int count = 0;

Ivar * ivar = class_copyIvarList ([self class], & count );

For (int I = 0; I <count; I ++ ){

// Retrieve the corresponding key based on each attribute. Note that the key value is the key of C language.

Ivar iva = ivar [I];

Const charchar * key = ivar_getName (iva );

// Convert to oc

NSString * strName = [NSString stringwithuf8string: key];

// Solve the problem

Id value = [aDecoder decodeObjectForKey: strName];

// Assign values to attributes using KVC

[Self setValue: value forKey: strName];

}

Free (ivar );

}

Return self;

}

// Archive and save data

-(Void) encodeWithCoder :( NSCoder *) ACO {

Unsigned int count;

Ivar * ivar = class_copyIvarList ([self class], & count );

For (int I = 0; I <count; I ++ ){

Ivar iv = ivar [I];

Const charchar * name = ivar_getName (iv );

NSString * strName = [NSString stringwithuf8string: name];

// Use the KVC Value

Id value = [self valueForKey: strName];

[Ecoder encodeObject: value forKey: strName];

}

Free (ivar );

}

One thing to note is that when Runtime is involved. Remember to release the memory. Xcode's ARC only applies to OC. Remember to manually free the pointer to C.

In addition, we will add the differences between class_copyPropertyList and class_copyIvarList:

Class_copyPropertyList only returns the attributes of the object class (attributes declared by @ property), while class_copyIvarList returns all attributes and variables of the class (including the variables declared in @ interface braces ), the following is a simple test. First, define a WFrequencyManager class, and then write a test function testProperties in the test class to call the above two functions to obtain their return results, and then traverse and output their return values respectively.

After the above test function is executed, the output result on the console is:

The preceding execution results show that the former only obtains the attribute declared by @ property, while the latter not only obtains the @ property attribute, but also obtains the variable declared in @ interface braces.

 

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.