Use of ios swift5 HandyJSON

Article directory

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

//
//  FDMomentModel.swift
//  FeiDao
//
//  Created by macvivi on 2022/1/14.
//

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 = ""
    //userId
    var userId = 0
    //tid:主题的id
    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)
                    }

Guess you like

Origin blog.csdn.net/baidu_40537062/article/details/124364820