YYModel是YY大神框架下面一個(gè)模型解析的框架,這里對(duì)實(shí)現(xiàn)不做介紹,僅僅介紹YYModel的用法.
1.首先,先貼一個(gè)json數(shù)據(jù)
{
"citys": [{
"id": 9,
"code": "100000",
"name": "北京市",
"baiduCode": "131",
"gaodeCode": "",
"tengxunCode": "",
"parentId": 8,
"level": 2,
"areas": [{
"id": 10,
"code": "100020",
"name": "朝陽(yáng)區(qū)",
"baiduCode": "",
"gaodeCode": "",
"tengxunCode": "",
"parentId": 9,
"level": 3,
"areas": null
}, {
"id": 13,
"code": "100089",
"name": "海淀區(qū)",
"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
}]
}
通過(guò)分析可知,這里屬于字典嵌套字典,所以需要實(shí)現(xiàn)modelContainerPropertyGenericClass協(xié)議
注意:
①這里一定要實(shí)現(xiàn)modelContainerPropertyGenericClass協(xié)議,否則解析出來(lái)areas數(shù)組將為nil.
②這里static一定不能漏掉,否則這個(gè)方法將不執(zhí)行,輸出將崩潰.
static func modelContainerPropertyGenericClass() -> [String : Any]? {
return ["areas":CityListModel.classForCoder()]
}
2.新建一個(gè)CityListModel的類,并遵守相應(yīng)的協(xié)議
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<UITouch>, 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里面添加保存數(shù)據(jù)和讀取數(shù)據(jù)的方法.最后完整的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]
}
}
最后控制臺(tái)輸出歸檔后的數(shù)據(jù)如下: