swift中YYModel的使用

YYModel是YY大神框架下面一个模型解析的框架,这里对实现不做介绍,仅仅介绍YYModel的用法.
1.首先,先贴一个json数据

{
        "citys": [{
            "id": 9,
            "code": "100000",
            "name": "北京市",
            "baiduCode": "131",
            "gaodeCode": "",
            "tengxunCode": "",
            "parentId": 8,
            "level": 2,
            "areas": [{
                "id": 10,
                "code": "100020",
                "name": "朝阳区",
                "baiduCode": "",
                "gaodeCode": "",
                "tengxunCode": "",
                "parentId": 9,
                "level": 3,
                "areas": null
            }, {
                "id": 13,
                "code": "100089",
                "name": "海淀区",
                "baiduCode": null,
                "gaodeCode": null,
                "tengxunCode": null,
                "parentId": 9,
                "level": 3,
                "areas": null
            }]
        }, {
            "id": 12,
            "code": "300000",
            "name": "天津市",
            "baiduCode": null,
            "gaodeCode": null,
            "tengxunCode": null,
            "parentId": 11,
            "level": 2,
            "areas": null
        }]
    }

通过分析可知,这里属于字典嵌套字典,所以需要实现modelContainerPropertyGenericClass协议
注意:
①这里一定要实现modelContainerPropertyGenericClass协议,否则解析出来areas数组将为nil.
②这里static一定不能漏掉,否则这个方法将不执行,输出将崩溃.

   static func modelContainerPropertyGenericClass() -> [String : Any]? {
        return ["areas":CityListModel.classForCoder()]
    }

2.新建一个CityListModel的类,并遵守相应的协议

class CityListModel: NSObject,NSCoding,YYModel {

    var areas : [CityListModel]?
    var baiduCode : String?
    var code : String?
    var gaodeCode : String?
    var ids : Int = 0
    var level : Int = 0
    var name : String?
    var parentId : Int = 0
    var tengxunCode : String?
    
        static func modelContainerPropertyGenericClass() -> [String : Any]? {
        return ["areas":CityListModel.classForCoder()]
    }
 }

3.使用yy_modelArray解析模型

    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
    
        let arr = self.getDataArr()
        let cityArr = NSArray.yy_modelArray(with: CityListModel.self, json: arr) as! [CityListModel]
        CityListModel.saveCityModel(cityArr: cityArr)
        sleep(2)
        print(CityListModel.getCityModelFromDie())

    }

4.最后在CityListModel里面添加保存数据和读取数据的方法.最后完整的CityListModel类如下:


class CityListModel: NSObject,NSCoding,YYModel {

    var areas : [CityListModel]?
    var baiduCode : String?
    var code : String?
    var gaodeCode : String?
    var ids : Int = 0
    var level : Int = 0
    var name : String?
    var parentId : Int = 0
    var tengxunCode : String?

    override init() {
        super.init()
    }
    
    required convenience init?(coder aDecoder: NSCoder) {
        self.init()
        self.yy_modelInit(with: aDecoder)
    }
    
    func encode(with aCoder: NSCoder) {
        self.yy_modelEncode(with: aCoder)
    }

    static func modelCustomPropertyMapper() -> [String : Any]? {
        return ["ids":"id"]
    }
    static func modelContainerPropertyGenericClass() -> [String : Any]? {
        return ["areas":CityListModel.classForCoder()]
    }
    
    override var description: String {
        return yy_modelDescription()
    }
    
    class func getSavePath() -> String{
        let docPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] as NSString
        let path = (docPath as String) + "/cityList.plist"
        print(path)
        return path
    }
    
    class func saveCityModel(cityArr:[CityListModel]) {
        let path = getSavePath()
        NSKeyedArchiver.archiveRootObject(cityArr, toFile: path)
    }
    
    class func getCityModelFromDie() -> [CityListModel]{
        let cityModel = NSKeyedUnarchiver.unarchiveObject(withFile: getSavePath())
        return cityModel as! [CityListModel]
    }
    
}

最后控制台输出归档后的数据如下:


swift中YYModel的使用_第1张图片
1.png

你可能感兴趣的:(swift中YYModel的使用)