1. Dictionary
data = {
author = {
avatar = "https://www.fiido.cc/storage/attachment/20220224/df930b0ec2942936b3d683e7425274ff.jpg" ;
comments = 42 ;
content = "\U9e3d\U5b50\U60c5\U7f18" ;
createdAt = "2022-02-23 19:44:18" ;
flag = 0 ;
gallery = (
"https://www.fiido.cc/storage/attachment/20220223/8c9ccccb7ff6e4cbab502942cc8bb055.jpg"
) ;
isFavorite = 1 ;
likes = 5 ;
nickname = Leon;
tid = 5 ;
userId = 3 ;
views = 358 ;
} ;
thread = {
concern = 2 ;
like = 1 ;
} ;
} ;
2. Model
import UIKit
import HandyJSON
class DetailMomentModel : NSObject , HandyJSON {
var avatar: String = ""
var comments = 1
var content: String = ""
var createdAt: String = ""
var gallery: [ String ] = [ ]
var isConcern = 0
var isLike = 0
var isFavorite = 0
var likes = 0
var nickname = ""
var userId = 0
var tid = 0
var views = 0
required override init ( ) {
}
func mapping ( mapper: HelpingMapper ) {
mapper <<<
self . avatar <-- "author.avatar"
mapper <<<
self . comments <-- "author.comments"
mapper <<<
self . content <-- "author.content"
mapper <<<
self . createdAt <-- "author.createdAt"
mapper <<<
self . gallery <-- "author.gallery"
mapper <<<
self . isConcern <-- "thread.concern"
mapper <<<
self . isLike <-- "thread.like"
mapper <<<
self . isFavorite <-- "author.isFavorite"
mapper <<<
self . likes <-- "author.likes"
mapper <<<
self . nickname <-- "author.nickname"
mapper <<<
self . userId <-- "author.userId"
mapper <<<
self . tid <-- "author.tid"
mapper <<<
self . views <-- "author.views"
}
}
3. Use
let dict: Dictionary = commonModel. data as ! Dictionary < String , Any >
if let model = DetailMomentModel . deserialize ( from: dict) {
printXY ( model, obj: self , line: #line )
}