Closed liudiange closed 4 years ago
由于是底层直接根据内存地址修改内存,所以不会触发didSet,不过没关系,取而代之的有2种做法
方法1:实现kj_didConvertToModel方法,在里面给expires_date赋值,参考监听
mutating func kj_didConvertToModel(from json: [String: Any]) {
expires_date = NSDate(timeIntervalSinceNow: expires_in)
}
方法2:直接将expires_in属性改为NSDate类型,KakaJSON内部会自动将毫秒数转为NSDate,参考Date
class UserAccount :NSObject, Convertible, NSCoding {
var expires_in : NSDate?
}
好的
刘殿阁 | |
---|---|
邮箱:shaoyeliudiange@163.com |
签名由 网易邮箱大师 定制
在2020年02月21日 10:05,M了个J 写道:
由于是底层直接根据内存地址修改内存,所以不会触发didSet,不过没关系,取而代之的有2种做法
方法1:实现kj_didConvertToModel方法,在里面给expires_date赋值,参考监听 mutatingfunckj_didConvertToModel(fromjson: [String: Any]) {
expires_date =NSDate(timeIntervalSinceNow: expires_in)
} 方法2:直接将expires_in属性改为NSDate类型,KakaJSON内部会自动将毫秒数转为NSDate,参考Date classUserAccount :NSObject, Convertible, NSCoding {
var expires_in : NSDate?
}
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.
老师 我最近用你的kakaJSON发现这样一个问题,就是不会触发didSet方法。我一个模型类是这样的
class UserAccount :NSObject, Convertible, NSCoding {
} 他不会触发 didSet{ expires_date = NSDate(timeIntervalSinceNow: expires_in) } 导致我的expires_date 没有值,但是我如果用字典转模型那样就是会触发didSet 这个方法 ,我知道原因是 字典转模型的时候 我先创建了这个模型然后在对他赋值的,所以才会触发。但是我想kakaJSON应该也会触发阿 ,拿到他在底层是不创建模型? 这是我调用kakaJSON 的地方 switch (response.result){ case.success(let json): let jsonObj = JSON.init(json).dictionaryValue let accoutModel = jsonObj.kj.model(UserAccount.self) finish(accoutModel) case .failure(let error): DGLog(error) finish(nil) } 我觉得我 应该是没用错呢 老师!望老师回答!