What is serialization? What is the role of serialization? How to achieve serialization in iOS?

table of Contents

 

Preface

What is serialization?

The role of serialization?

How to achieve serialization in iOS?

For example

Things to note about serialization


Preface

In our development process, we will more or less come into contact with the term serialization , and some people have used serialization operations, but they don't know that this operation is called serialization.

What is serialization?

Serialization (Serialization) The process of converting the state information of an object into a form that can be stored or transmitted. During serialization, the object writes its current state to temporary or persistent storage. Later, you can recreate the object by reading or deserializing the state of the object from the storage area.

Serialization enables other code to view or modify the object instance data that cannot be accessed without serialization. To be precise, code execution serialization requires special permissions: SecurityPermission with the SerializationFormatter flag specified. Under the default policy, code downloaded over the Internet or Internet code does not grant this permission; only code on the local computer is granted this permission.

The role of serialization?

  1. Make custom objects persistent in some storage form ;
  2. Pass objects from one place to another.
  3. Make the program more maintainable.

How to achieve serialization in iOS?

In the actual development of iOS, we will also use serialization. Many people will say, no? Do we have the word serialization in iOS?

Then I will say another word, "Archive", have you used it? Archiving is where we use serialization in iOS development!

In iOS, a custom object cannot be stored directly in a file, it must be converted into a binary stream first. The process from an object to binary data is generally referred to as serialization of the object (Serialization), also known as archive (Archive). Similarly, the process from binary data to an object is generally called deserialization or anti-archiving (unarchiving).

NSCoding and NSCopying (not required) protocols need to be implemented in serialization

For example

  • Create a Person class

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject<NSCoding>

@property (copy,   nonatomic) NSString *name;
@property (copy,   nonatomic) NSString *sex;

@end

Person.m

#import "Person.h"

@implementation Person
#if 0
//归档
- (void)encodeWithCoder:(NSCoder *)aCoder {

	[aCoder encodeObject:self.name forKey:@"name"];
	[aCoder encodeInteger:self.age forKey:@"age"];
}
//解档
- (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder {

	self = [super init];
	if (self) {
		self.name = [aDecoder decodeObjectForKey:@"name"];
		self.age  = [aDecoder decodeIntegerForKey:@"age"];
	}
	return self;
}
  • Operation in the controller

  Serialization (Archive)

 Person *person = [[Person alloc] init];
	person.name = @"Frank";
	person.age  = 18;
		//这里以temp路径为例,存到temp路径下
	NSString *temp = NSTemporaryDirectory();
	NSString *filePath = [temp stringByAppendingPathComponent:@"obj.data"]; //注:保存文件的扩展名可以任意取,不影响。
	NSLog(@"%@", filePath);
		//归档
	BOOL isSuccess = [NSKeyedArchiver archiveRootObject:person toFile:filePath];
	if(isSuccess) {
			NSLog(@"归档成功");
		}else{
			NSLog(@"归档失败");
		}

 Code running results:

Standard archive

 

Find the archived file according to the printed path

 

 

Deserialization (unarchive)

NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"obj.data"];
	// 解档
	Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
	NSLog(@"name:%@,age:%ld",person.name,person.age);

 

Code running results:

Unarchive

 

 

Things to note about serialization

  1.   Serialization only saves the state of the object, regardless of the method of the object.
  2.   When a parent class implements serialization, its subclasses also automatically implement serialization, and are implemented without display.
  3.   When an instance object references other objects, the referenced object is also instantiated when the object is serialized.

 

 

Guess you like

Origin blog.csdn.net/zjpjay/article/details/86518545