-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJXAutoEncoder.swift
More file actions
40 lines (31 loc) · 966 Bytes
/
JXAutoEncoder.swift
File metadata and controls
40 lines (31 loc) · 966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// JXAutoEncoder.swift
// JXAutoEncoder
//
// Created by jx on 2017/2/9.
// Copyright © 2017年 jx. All rights reserved.
//
import Foundation
open class JXAutoEncoder: NSObject, NSCoding {
// MARK:- 处理需要归档的字段
public func encode(with aCoder:NSCoder) {
let mirror = Mirror(reflecting: self)
for case let (label?, value) in mirror.children {
aCoder.encode(value, forKey: label)
}
}
// MARK:- 处理需要解档的字段
required public init(coder aDecoder:NSCoder) {
super.init()
let mirror = Mirror(reflecting: self)
for case let (label?, _) in mirror.children {
guard aDecoder.decodeObject(forKey: label) != nil else {
return
}
setValue(aDecoder.decodeObject(forKey: label), forKey: label)
}
}
override public init() {
super.init()
}
}