布袋的世界之Apple苹果家园swift

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

2017-06-29  本文已影响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)

哈,就这些了!

上一篇下一篇

猜你喜欢

热点阅读