美文网首页布袋的世界之Apple苹果家园swift
使用HandyJSON进行归档解档类的写法

使用HandyJSON进行归档解档类的写法

作者: 布袋的世界 | 来源:发表于2017-06-29 14:39 被阅读43次

// HandyJSON写在NSObject后面
class LotteryFocusDataList:NSObject, HandyJSON,NSCoding{
    
    var image:String?
    var link:String?
    var id:Int?
    
    init(dict: [String: Any]) {
        super.init()
        setValuesForKeys(dict)
    }
    override func setValue(_ value: Any?, forUndefinedKey key: String) {}
    // MARK:- 处理需要归档的字段
    func encode(with aCoder:NSCoder) {
        aCoder.encode(image, forKey:"image")
        aCoder.encode(link, forKey:"link")
        aCoder.encode(id, forKey:"id")
    }
    // MARK:- 处理需要解档的字段
    required init(coder aDecoder:NSCoder) {
        super.init()
        image = (aDecoder.decodeObject(forKey:"image")as? String)!
        link = (aDecoder.decodeObject(forKey:"link")as? String)!
        id = (aDecoder.decodeObject(forKey:"id")as? Int)!
    }
    
    override required init() {
        super.init()
    }
}

归档、解档 统一在 common.swift

/// 沙盒归档路径
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] as NSString
let SELECTED_CHANNELS: String = "selectedChannels.archive" // 选择频道列表
let UNSELECTED_CHANNELS: String = "unselectedChannels.archive" // 未选择频道列表

/// 初始化common
let common = Common()

class Common: NSObject {
 
    // MARK: -解档归档(保存的是LotteryFocusDataList数组)
    // 归档
    func archiveData(channel: [LotteryFocusDataList], appendPath: String) {
        let filePath = path.appendingPathComponent(appendPath)
        NSKeyedArchiver.archiveRootObject(channel, toFile: filePath)
    }
    
    //反归档
    func unarchiveData(appendPath: String) -> ([LotteryFocusDataList]?) {
        let filePath = path.appendingPathComponent(appendPath)
        return NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as? [LotteryFocusDataList]
    }
    
    // MARK: -解档归档(保存的是String数组)
    // 归档
    func archiveWithStringArray(channel: [String], appendPath: String) {
        let filePath = path.appendingPathComponent(appendPath)
        NSKeyedArchiver.archiveRootObject(channel, toFile: filePath)
    }
    
    //反归档
    func unarchiveToStringArray(appendPath: String) -> ([String]?) {
        let filePath = path.appendingPathComponent(appendPath)
        return NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as? [String]
    }
    
    // MARK: -解档归档(保存的是任意对象数组)
    func archive(array: [AnyObject], appendPath: String) {
        let filePath = path.appendingPathComponent(appendPath)
        NSKeyedArchiver.archiveRootObject(array, toFile: filePath)
    }
    
    func unarchive(appendPath: String) -> ([AnyObject]?) {
        let filePath = path.appendingPathComponent(appendPath)
        return NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as! ([AnyObject]?)
    }
    
    // MARK: -  创建一个barButtonItem
    class func itemWithImage(_ image:UIImage,highlightImage:UIImage,target:UIViewController,action:Selector) -> UIBarButtonItem{
        let button = UIButton.init()
        button.setBackgroundImage(image, for: UIControlState())
        button.setBackgroundImage(highlightImage, for: .highlighted)
        button.sizeToFit()
        button.addTarget(target, action: action, for: .touchUpInside)
        return UIBarButtonItem.init(customView: button)
    }
    
}

viewController.swift 使用

// 图片数组
 lazy var  FOCUS_IMAGES_CHANEL =  [LotteryFocusDataList]()
// 解档 
 let LOCAL_FOCUS_ARCHIVE = common.unarchiveData(appendPath: FOCUS_IMAGES_ARCHIVE)
// 归档
common.archiveData(channel: (self?.FOCUS_IMAGES_CHANEL)!, appendPath: FOCUS_IMAGES_ARCHIVE)

哈,就这些了!

相关文章

  • 使用HandyJSON进行归档解档类的写法

    类 归档、解档 统一在 common.swift viewController.swift 使用 哈,就这些了!

  • 归档/解档

    一、对系统类进行归档/解档 第一种:对集合类对象进行归档/解档 归档: 解档 方法(1) 方法(2) 第二种:非集...

  • iOS 13归档解档

    归档解档的使用 自定义类对象要进行归档,那么这个对象的属性所属的类必须要遵守归档协议NSCoding必须在需要归档...

  • runtime 进行归档和解档

    数据本地持久化时, 一般会将模型进行归档, 从本地获取数据时, 需要解档下面使用runtime的方式进行归档解档,...

  • 归档与解档

    归档:将对象按照一定的格式保存到文件中;解档:从文件中还原对象的过程 官方类的归档与解档方式一: 官方类的归档与解...

  • 使用runtime 进行归档和解档

    使用runTime进行归档与解档 //归档 使用编码器将对象编码成二进制流 -(void)encodeWithCo...

  • 数据存储-归档解档

    1.介绍 2.注意: 3.归档对象的写法 4.归档 -- NSKeyedArchiver 5.解档 -- NSKe...

  • iOS - runtime-04实现自动解归档

    通过 runtime 进行归档、解档很节省很多工作,我先贴一段常规的解归档的代码。 通过这种方式进行解归档很麻烦,...

  • iOS数据本地存储方法

    归档解档(使用MJExtension,模型,数组) MJExtension里面已经给我们写好了归档接档的方法,首先...

  • IOS本地存储的四种方式

    概要 一、NSKeyedArchiver归档(NSCoding)序列化 需要归档解档的类需要遵守NSCoding协...

网友评论

    本文标题:使用HandyJSON进行归档解档类的写法

    本文链接:https://www.haomeiwen.com/subject/dfbmcxtx.html