归档、解档

我对归档和解档的理解是

归档就是把要保存的内容转成Data之后写入指定的路径中。
解档就是把指定路径下的Data转成我们需要的数据类型。

对象的归档、解档

iOS 12.0以后一个安全归档协议NSSecureCoding继承自NSCoding,多了一个static var supportsSecureCoding: Bool属性。

Student类,继承自`NSObject`,遵循`NSSecureCoding`协议。
class Student: NSObject, NSSecureCoding {
    var name: String?
    var age: Int? = 1
    var height: CGFloat = 0.5
    var weight: CGFloat = 5.0
    
    init(name: String? = nil, age: Int? = nil, height: CGFloat = 1.80, weight: CGFloat = 75) {
        self.name = name
        self.age = age
        self.height = height
        self.weight = weight
        super.init()
    }
    
    /// NSSecureCoding
    static var supportsSecureCoding: Bool = true
    func encode(with coder: NSCoder) {
        coder.encode(name, forKey: "name")
        coder.encode(age, forKey: "age")
        coder.encode(height, forKey: "height")
        coder.encode(weight, forKey: "weight")
    }
    required init?(coder: NSCoder) {
        name = coder.decodeObject(forKey: "name") as? String
        age = coder.decodeObject(forKey: "age") as? Int
        height = coder.decodeObject(forKey: "height") as! CGFloat
        weight = coder.decodeObject(forKey: "weight") as! CGFloat
        super.init()
    }
    override var description: String {
        return "name: \(self.name), age: \(self.age), height: \(self.height), weight: \(self.weight)"
    }
}

Student归档和解档

/// 要归档对象
let stu = Student()
/// 归档路径 <路径必须是通过`fileURLWithPath`这个函数创建的>
let pathStu = (NSTemporaryDirectory() as NSString).appendingPathComponent("student.plist")
let urlStu = URL(fileURLWithPath: pathStu)
if #available(iOS 11.0, *) {
    do {
        /// 归档对象转 Data
        let data = try NSKeyedArchiver.archivedData(withRootObject: stu, requiringSecureCoding: true)
        ///  写进置顶路径
        try data.write(to: urlStu)
    } catch {
        print("stu 失败 \(error)")
    }
} else {
    let success = NSKeyedArchiver.archiveRootObject(stu, toFile: pathStu)
    print("succ == \(success)")
}

/// 解档
do {
    /// 获取路径下的Data
    let dataStu = try Data(contentsOf: urlStu)
    var stu: Student
    /// 把Data 转成 对象
    if #available(iOS 11.0, *) {
        stu = try NSKeyedUnarchiver.unarchivedObject(ofClass: Student.self, from: dataStu)!
    } else {
        stu = NSKeyedUnarchiver.unarchiveObject(with: dataStu) as! Student
    }
    print("\(stu.name), \(stu.score),\(stu.height)")
} catch {
    
}

数组、字典、字符串等的归档<本地存储>

func archiveSmapleDemo(){
    let arr = ["123","111"]
    /// 获取本地存储的路径
    let pathArr = (NSTemporaryDirectory() as NSString).appendingPathComponent("arr.txt")
    let urlArr = URL(fileURLWithPath: pathArr)
    do {
        /// 转成Data 并且写入数据
        let data = try JSONSerialization.data(withJSONObject: arr, options: .prettyPrinted)
        try data.write(to: urlArr)
    } catch {
        
    }
    
    do {
        /// 根据路径取出Data
        let dataArr = try Data(contentsOf: urlArr)
        /// 解析成数组
        let arr = try JSONSerialization.jsonObject(with: dataArr, options: .mutableContainers) as! [String]
        print(arr)
    } catch {
        
    }
}

自定义对象数组归档、解档

func archiveSmapleDemo(){
    let s1 = Student()
    let s2 = Student()
    let s3 = Student()
    
    let arr = [s1,s2,s3]
    let pathArr = (NSTemporaryDirectory() as NSString).appendingPathComponent("arr.data")
    let urlArr = URL(fileURLWithPath: pathArr)
    /// 归档
    do {
        let data: Data
        /// 转成Data 并且写入本地
        /// 自定义对象要用 `NSKeyedArchiver`转成Data
        if #available(iOS 11.0, *) {
            data = try NSKeyedArchiver.archivedData(withRootObject: arr, requiringSecureCoding: true)
        } else {
            data = NSKeyedArchiver.archivedData(withRootObject: arr)
        }
        try data.write(to: urlArr)
    } catch {
        print("error == \(error)")
    }
    /// 解档
    do {
        /// 根据路径取出Data
        let dataArr = try Data(contentsOf: urlArr)
        if #available(iOS 14.0, *) {
            /// 直接解档成 对象数组
            if let arr = try NSKeyedUnarchiver.unarchivedArrayOfObjects(ofClass: Student.self, from: dataArr) {
                arr.forEach { p in
                    print("p.desc == \(p.description)")
                }
            }else {
                
            }
        } else {
            /// 解档之后转换成对象数组
            if let arr = NSKeyedUnarchiver.unarchiveObject(with: dataArr) as? [Student]{
                arr.forEach { p in
                    print("p.desc == \(p.description)")
                }
            }
        }
    } catch {
        
    }
}

学习资料:
iOS数组里为自定义对象情况下的归档

你可能感兴趣的:(归档、解档)