字典转模型
开发中网络请求成功,通常情况下三方(AFNetWorking / Alamofire)都会自动帮我们解析json为字典返回我们。而实际开发中我们是主张面对模型开发而非面对字典,这就需要将拿到的字典转换成我们想要的模型。OC中字典转模型有很多非常成熟的三方,如:MJExtension、JsonModel等。而在Swift中如果不是特别复杂的字典我们基本可以利用KVC直接将其转换成模型
模型一
class JYModelOne: NSObject, NSCoding {
/// 姓名
var name: String?
/// 年龄
var age: String?
/// 爱好
var hobby: [String]?
/// 自定义对象
var friend: JYModelTwo?
}
模型二
class JYModelTwo: NSObject {
var height: String?
var weight: String?
}
这里有两个模型,其中模型JYModelOne中有一个属性是模型二JYModelTwo类型的属性,简单实现下“模型套模型”的字典转模型(两层的嵌套在平时开发中基本够用)
class JYModelOne: NSObject, NSCoding {
/// 姓名
var name: String?
/// 年龄
var age: String?
/// 爱好
var hobby: [String]?
/// 自定义对象
var friend: JYModelTwo?
init(dict: [String: Any]) {
super.init()
// KVC赋值
setValuesForKeys(dict)
}
override func setValue(_ value: Any?, forKey key: String) {
if key == "friend" {// 如果是自定义对象特殊处理
if let dict = value as? [String: Any] {
friend = JYModelTwo(dict: dict)
}
}else {// 基本数据类型直接KVC赋值
super.setValue(value, forKey: key)
}
}
/// 字典中有的key,但是model中没有对应的属性,在赋值的时候就会调用这个方法,空实现,不写会崩溃
override func setValue(_ value: Any?, forUndefinedKey key: String) {}
class JYModelTwo: NSObject {
var height: String?
var weight: String?
init(dict: [String: Any]) {
super.init()
// KVC赋值
setValuesForKeys(dict)
}
override func setValue(_ value: Any?, forKey key: String) {
// 基本数据类型直接KVC赋值
super.setValue(value, forKey: key)
}
/// 字典中有的key,但是model中没有对应的属性,在赋值的时候就会调用这个方法,空实现,不写会崩溃
override func setValue(_ value: Any?, forUndefinedKey key: String) {}
}
runtime帮助实现归、解档
说到归档其实无非就是遵守NSCoding协议,实现两个方法,倒也没什么难,但麻烦的是什么呢?如果一个对象有特别多的属性,不多说直接上代码亲自感受......
/// 登录用户信息
class CCUserinfo: NSObject, NSCoding {
/// 用户唯一标识 uid(用户体系统一用)
var uid: String?
var id: String?
/// 注册类型 1:pad app 2:虫钢网站 3:第三方注册
var register_type: String?
/// 用户绑定email
var email: String?
/// 用户虫钢用户名
var username: String?
/// 用户手机号
var user_phone: String?
/// 用户头像地址
var head_portrait_image: String?
/// 是否已验证手机 0:未验证 1:已验证
var is_phone: String?
/// 昵称
var nickname: String?
/// 性别 0:位置 1:男 2:女
var gender: String?
/// 出生年月日
var birthday: String?
/// 所在省份
var province: String?
/// 所在城市
var city: String?
/// 所在县
var area: String?
/// 用户简介
var personal_profile: String?
/// 用户状态
var status: String?
/// 校验手机时间
var checktime: String?
/// 注册时间
var addtime: String?
}
通常归档写法
func encode(with aCoder: NSCoder) {
aCoder.encode(uid, forKey: "uid")
aCoder.encode(id, forKey: "id")
aCoder.encode(register_type, forKey: "register_type")
aCoder.encode(email, forKey: "email")
aCoder.encode(username, forKey: "username")
aCoder.encode(user_phone, forKey: "user_phone")
aCoder.encode(head_portrait_image, forKey: "head_portrait_image")
aCoder.encode(is_phone, forKey: "is_phone")
aCoder.encode(nickname, forKey: "nickname")
aCoder.encode(gender, forKey: "gender")
aCoder.encode(birthday, forKey: "birthday")
aCoder.encode(province, forKey: "province")
aCoder.encode(city, forKey: "city")
aCoder.encode(area, forKey: "area")
aCoder.encode(personal_profile, forKey: "personal_profile")
aCoder.encode(status, forKey: "status")
aCoder.encode(checktime, forKey: "checktime")
aCoder.encode(addtime, forKey: "addtime")
}
required init?(coder aDecoder: NSCoder) {
super.init()
uid = aDecoder.decodeObject(forKey: "uid") as? String
id = aDecoder.decodeObject(forKey: "id") as? String
register_type = aDecoder.decodeObject(forKey: "register_type") as? String
email = aDecoder.decodeObject(forKey: "email") as? String
username = aDecoder.decodeObject(forKey: "username") as? String
user_phone = aDecoder.decodeObject(forKey: "user_phone") as? String
head_portrait_image = aDecoder.decodeObject(forKey: "head_portrait_image") as? String
is_phone = aDecoder.decodeObject(forKey: "is_phone") as? String
nickname = aDecoder.decodeObject(forKey: "nickname") as? String
gender = aDecoder.decodeObject(forKey: "gender") as? String
birthday = aDecoder.decodeObject(forKey: "birthday") as? String
province = aDecoder.decodeObject(forKey: "province") as? String
city = aDecoder.decodeObject(forKey: "city") as? String
area = aDecoder.decodeObject(forKey: "area") as? String
personal_profile = aDecoder.decodeObject(forKey: "personal_profile") as? String
status = aDecoder.decodeObject(forKey: "status") as? String
checktime = aDecoder.decodeObject(forKey: "checktime") as? String
addtime = aDecoder.decodeObject(forKey: "addtime") as? String
}
如果有100个属性我们就要在init和encode方法中把100个属性都写到里面,以后给对象添加、删减属性,还要对应的去修改init和encode方法中的代码,麻烦且容易出错,代码看着也很臃肿。不妨考虑下runtime动态获取对象属性,再利用KVC赋值,最后进行对象归档和解档,这样方便很多,以后迭代中给对象添加、删减属性就不再考虑修改init和encode函数中代码。
// 解档
required init?(coder aDecoder: NSCoder) {
super.init()
// 1.动态获取对象所有成员变量
var count: UInt32 = 0
let propertyArray = class_copyPropertyList(JYModelOne.self, &count)
for i in 0..<Int(count) {
// 2.1、根据下表获取属性
let property = propertyArray?[i]
// 2.2、获取属性名称(c语言字符串)
guard let cName = property_getName(property) else {
return
}
guard let name = String(utf8String: cName) else {
return
}
// 3、解档
if name == "friend" {// 自定义对象特殊处理
friend = JYModelTwo.unarchive()
}else {
let value = aDecoder.decodeObject(forKey: name)
setValue(value, forKey: name)
}
}
}
// 归档
func encode(with aCoder: NSCoder) {
// 1.动态获取对象所有成员变量
var count: UInt32 = 0
let propertyArray = class_copyPropertyList(JYModelOne.self, &count)
for i in 0..<Int(count) {
// 2.1、根据下表获取属性
let property = propertyArray?[i]
// 2.2、获取属性名称(c语言字符串)
guard let cName = property_getName(property) else {
return
}
guard let name = String(utf8String: cName) else {
return
}
// 3、归档
if name == "friend" {// 自定义对象特殊处理
if let modelTwo = self.value(forKey: name) as? JYModelTwo {
modelTwo.archive()
}
}else {
let value = self.value(forKey: name)
aCoder.encode(value, forKey: name)
}
}
}
主要部分实现完成,但实际开发中如果需要将对象进行归、解档实现时建议将具体的操作封装成对应的函数开放出来,这样外部要实现归、解档只需调对应函数,而不用管什么实现逻辑,充分利用面向对象思想,谁的事情就让谁来做。
extension JYModelOne {
/// 路径
fileprivate class func getArchivePath() -> (String) {
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first!
return path + "/JYModelOne.plist"
}
/// 归档
func archive() -> () {
NSKeyedArchiver.archiveRootObject(self, toFile: JYModelOne.getArchivePath())
}
/// 解档
class func unarchive() -> (JYModelOne?) {
return NSKeyedUnarchiver.unarchiveObject(withFile: JYModelOne.getArchivePath()) as? JYModelOne
}
/// 删除归档信息
class func remove(complete: () -> ()) -> () {
if FileManager.default.fileExists(atPath: JYModelOne.getArchivePath()) == false {
return
}
do {
try FileManager.default.removeItem(atPath: JYModelOne.getArchivePath())
}catch {}
complete()
}
}
网友评论