Swift-自动归档存储用户信息

import UIKit

@objcMembers//// 在swift 4.0 之后编辑器是不会对属性加 隐式的 @objc 的 如果不加 @objcMembers 运行时是获取不到属性和ivar 的
class BaseCodingModel: NSObject, NSCoding {

    override init() {
        super.init()
    }
    
    // MARK: - 归档
    func encode(with aCoder: NSCoder) {
        for name in CodeUserModel.propertyList() {
            guard let value = self.value(forKey: name) else {
                return
            }
            aCoder.encode(value, forKey: name)
        }
    }
    // MARK: - 解档
    required init?(coder aDecoder: NSCoder) {

    }
    
    // MARK: - 获取属性数组
    class func propertyList() -> [String] {
        var count: UInt32 = 0 // 下面会改变count 所以要用 var
        // 需要的是一个 UInt32 类型的指针
        // 在 class 方法中 self 指的是本类  不是本类对象
        let list = class_copyPropertyList(self, &count)
        
        var arrayName:[String] = []
        
        //此时 count 是 UInt32类型的 list是 '属性' 的 数组
        for i in 0.. [String] {
        // 于获取属性类似
        var count: UInt32 = 0
        let list = class_copyIvarList(self, &count)
        
        var arrayName:[String] = []
        
        for i in 0..
import UIKit

class CodeUserModel: BaseCodingModel {
    //单列
    static let sharedUserModel = CodeUserModel()
    
    var isLogin: String = "false"
    var customer_id: String = ""
    var mobile: String = ""
    var token: String = ""
    
    private let userModelKey = "myUserModel"
    
    override init() {
        super.init()
    }
    
    // MARK: - 解档
    required init?(coder aDecoder: NSCoder) {
        super.init()
        for name in CodeUserModel.propertyList() {
            guard let value = aDecoder.decodeObject(forKey: name) else {
                return
            }
            setValue(value, forKeyPath: name)
        }
    }
    
    // MARK: 查询
    func unarchive () {
        do {
            let dataFrom = kUserDefaults.data(forKey: userModelKey)
            if dataFrom != nil {
                if let codeUser = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(dataFrom!) as? CodeUserModel {
                    customer_id = codeUser.customer_id
                    mobile = codeUser.mobile
                    token = codeUser.token
                    isLogin = codeUser.isLogin
                    printLog(message: "查询用户信息---\(customer_id)---\(mobile)---\(token)---\(isLogin)")
                }
            }
        } catch {
            printLog(message: "获取失败了")
        }
    }
    
    // MARK: 保存
    func archive(user:CodeUserModel) {
        let data = NSKeyedArchiver.archivedData(withRootObject: user)
        kUserDefaults.set(data, forKey: userModelKey)
        kUserDefaults.synchronize()
    }
    
    // MARK: 退出登录
    func logout() {
        //清空数据
        let data = NSKeyedArchiver.archivedData(withRootObject: CodeUserModel())
        kUserDefaults.set(data, forKey: userModelKey)
        kUserDefaults.synchronize()
    }
}

//读取信息
let useModel = CodeUserModel.sharedUserModel
useModel.unarchive()

//登录保存信息
var userModel:CodeUserModel = OCUserModel.sharedUserModel
userModel = CodeUserModel.mj_object(withKeyValues: responseObject)
userModel.isLogin = "true"
codeUser.archive(user: codeUser)

//退出登录
CodeUserModel.sharedUserModel.logout()

你可能感兴趣的:(Swift-自动归档存储用户信息)