swift中数据持久化之归档解档 NSKeyedArchiver NSKeyedUnarchiver

最近做了一个需求,将数据保存到沙盒中来实现数据持久化,写了一个manager,供参考:

import Foundation

class TaxiCertifyInfoManager {
    
    // MARK: - Static
    static let `dafault` = TaxiCertifyInfoManager()
    
    // MARK: - Property
    var identifyInfoModel = TaxiCertifyInfoModel()
    
    // MARK: - Life Cycle
    init() {
//        getDataFromDocument()
    }
    
    // MARK: - Pulice Func
    func save() {
        // 保存至硬盘文件
        let userNotificationSavedPath = "\(NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,FileManager.SearchPathDomainMask.userDomainMask,true).first!)/\(UserManager.default.cid?.md5() ?? "")/identifyInfo.data"
        NSKeyedArchiver.archiveRootObject(identifyInfoModel, toFile:userNotificationSavedPath)
    }

    ///读取数据
    func getDataFromDocument() {
        let fileManager = FileManager.default
        let localPath = "\(NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,FileManager.SearchPathDomainMask.userDomainMask,true).first!)/\(UserManager.default.cid?.md5() ?? "")"
        
        if !fileManager.fileExists(atPath: localPath) {
            try? fileManager.createDirectory(atPath: localPath, withIntermediateDirectories: true, attributes: nil)
        }
        let userNotificationSavedPath = "\(NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,FileManager.SearchPathDomainMask.userDomainMask,true).first!)/\(UserManager.default.cid?.md5() ?? "")/identifyInfo.data"
        let identifyInfoM = NSKeyedUnarchiver.unarchiveObject(withFile:userNotificationSavedPath) as? TaxiCertifyInfoModel
 
        // 填充注册信息
        if identifyInfoM != nil {
            IdentityVM.default.personalInfoVM.firstName = identifyInfoM?.first_name
            IdentityVM.default.personalInfoVM.lastName = identifyInfoM?.last_name
            IdentityVM.default.personalInfoVM.gender = identifyInfoM?.gender
            IdentityVM.default.personalInfoVM.IDNumber = identifyInfoM?.id_card_no
            IdentityVM.default.personalInfoVM.firstGetDriversLicenseDate = identifyInfoM?.get_license_date
            IdentityVM.default.personalInfoVM.driversLicenseUrl = identifyInfoM?.license_img 
            
            IdentityVM.default.companyVM.cityModel?.province_name = identifyInfoM?.car_province_name
            IdentityVM.default.companyVM.cityModel?.city_name = identifyInfoM?.car_city_name
            
            IdentityVM.default.companyVM.companyModel?.id = identifyInfoM?.company_id
            IdentityVM.default.companyVM.companyModel?.name = identifyInfoM?.company_name
            
            if let str = identifyInfoM?.car_no {
                IdentityVM.default.vehicleVM.vehicleFront =  String(str.prefix(1))
                IdentityVM.default.vehicleVM.locationAbbr = String(str[str.index(str.startIndex, offsetBy: 1)])
                IdentityVM.default.vehicleVM.vehicleNumber = String(str[str.index(str.startIndex, offsetBy: 2)..

下面是model的类,其中的encode和decode是第一次在swift中写。

@objc(notification) class TaxiCertifyInfoModel : NSObject, NSCoding {
    
    var certify_state : IdentifyStatus?        // 认证状态(0-未提交审核 1-审核中 2-审核失败 3-审核通过 4-停止服务(黑名单))

    /*第一页*/
    var license_img : String? { // 驾照图片URL
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
    var first_name : String? { // 真实姓
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
    var last_name : String? { // 真实名
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
    var gender : Int? { // 性别(1-男 2-女)
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
   
    var id_card_no : String? {  // 身份证号
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
    var get_license_date : String? { // 初次领取驾照日期(yyyy-MM-dd)
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
   
    /*第二页*/
    var car_license_img: String? { // 行驶证图片URL
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
    
    var car_no: String? { // 车牌号
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
    var car_reg_date: String? { // 车辆注册日期(yyyy-MM-dd)
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
    var car_owner: String? {  // 车辆所有人
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
    
    /*第三页*/
    var car_province_name : String? { // 车辆所属省份
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
    
    var car_district_name: String? { // 车辆所属地区
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
    var company_name : String? { // 出租车公司名
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
    var driver_permit_no:String? { // 准驾号码
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
    var driver_permit_img: String? { // 准驾证图片URL
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
    var car_photo: String? { // 人车合影URL
        didSet {
            TaxiCertifyInfoManager.dafault.save()
        }
    }
   
    /*暂时不用*/
    var car_district_id: Int?       // 车辆所属地区id
    var car_city_name: String?      // 车辆所属城市
    var company_id : Int?           // 出租车公司id
    

    func encode(with aCoder: NSCoder) {
        aCoder.encode(certify_state, forKey: Constant.certify_state)
        aCoder.encode(first_name, forKey: Constant.first_name)
        aCoder.encode(last_name, forKey: Constant.last_name)
        aCoder.encode(gender, forKey: Constant.gender)
        aCoder.encode(id_card_no, forKey: Constant.id_card_no)
        aCoder.encode(get_license_date, forKey: Constant.get_license_date)
        aCoder.encode(license_img, forKey: Constant.license_img)
        aCoder.encode(car_province_name, forKey: Constant.car_province_name)
        aCoder.encode(car_district_id, forKey: Constant.car_district_id)
        aCoder.encode(car_district_name, forKey: Constant.car_district_name)
        aCoder.encode(car_city_name, forKey: Constant.car_city_name)
        aCoder.encode(company_id, forKey: Constant.company_id)
        aCoder.encode(company_name, forKey: Constant.company_name)
        aCoder.encode(car_no, forKey: Constant.car_no)
        aCoder.encode(car_reg_date, forKey: Constant.car_reg_date)
        aCoder.encode(car_owner, forKey: Constant.car_owner)
        aCoder.encode(car_license_img, forKey: Constant.car_license_img)
        aCoder.encode(car_photo, forKey: Constant.car_photo)
        aCoder.encode(driver_permit_no, forKey: Constant.driver_permit_no)
        aCoder.encode(driver_permit_img, forKey: Constant.driver_permit_img)
    }
    
    override init() {
        super.init()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init()
        certify_state = aDecoder.decodeObject(forKey: Constant.certify_state) as? IdentifyStatus
        first_name = aDecoder.decodeObject(forKey: Constant.first_name) as? String
        last_name = aDecoder.decodeObject(forKey: Constant.last_name) as? String
        gender = aDecoder.decodeObject(forKey: Constant.gender) as? Int
        id_card_no = aDecoder.decodeObject(forKey: Constant.id_card_no) as? String
        get_license_date = aDecoder.decodeObject(forKey: Constant.get_license_date) as? String
        license_img = aDecoder.decodeObject(forKey: Constant.license_img) as? String
        car_province_name = aDecoder.decodeObject(forKey: Constant.car_province_name) as? String
        car_district_id = aDecoder.decodeObject(forKey: Constant.car_district_id) as? Int
        car_district_name = aDecoder.decodeObject(forKey: Constant.car_district_name) as? String
        car_city_name = aDecoder.decodeObject(forKey: Constant.car_city_name) as? String
        company_id = aDecoder.decodeObject(forKey: Constant.company_id) as? Int
        company_name = aDecoder.decodeObject(forKey: Constant.company_name) as? String
        car_no = aDecoder.decodeObject(forKey: Constant.car_no) as? String
        car_reg_date = aDecoder.decodeObject(forKey: Constant.car_reg_date) as? String
        car_owner = aDecoder.decodeObject(forKey: Constant.car_owner) as? String
        car_license_img = aDecoder.decodeObject(forKey: Constant.car_license_img) as? String
        car_photo = aDecoder.decodeObject(forKey: Constant.car_photo) as? String
        driver_permit_no = aDecoder.decodeObject(forKey: Constant.driver_permit_no) as? String
        driver_permit_img = aDecoder.decodeObject(forKey: Constant.driver_permit_img) as? String
    }
}

你可能感兴趣的:(swift中数据持久化之归档解档 NSKeyedArchiver NSKeyedUnarchiver)