美文网首页
iOS 【Swift-ObjectMapper对象转换】

iOS 【Swift-ObjectMapper对象转换】

作者: NJ_墨 | 来源:发表于2021-08-12 17:14 被阅读0次

    json字符串转对象

    class NewRespModel:Mappable{
        required init?(map: Map) {
        }
        func mapping(map: Map) {
            result <- map["result"]
            statusCode <- map["code"]
            message <- map["message"]
        }
        
        var result: NewResultModel?
        var statusCode:Int?
        var message:String?
    }
    
    class NewResultModel: Mappable {
        required init?(map: Map) {
        }
        
        func mapping(map: Map) {
            protogene <- map["protogene"]
            id <- map["id"]
        }
        
        var protogene:[ProtoModel]?
        var id:String?
    }
    
    class ProtoModel:Mappable {
    
        required init?(map: Map) {
        }
        
        func mapping(map: Map) {
            bg_color <- map["bg_color"]
            sort <- map["sort"]
        }
        
        var bg_color:String?
        var sort:String?
    }
    
    let resp = NewRespModel(JSONString: reqJson)
    

    json字符串转数组

    class PlatformModel: NSObject, Mappable {
        required init?(map: Map) {
        }
        func mapping(map: Map) {
            social_name <- map["social_name"]
            icon_link <- map["icon_link"]
        }
        @objc var social_name: NSString?
        @objc var icon_link: NSString?
    }
    
    let results = Mapper<PlatformModel>().mapArray(JSONObject: reqDic["result"]) as NSArray?
    

    相关文章

      网友评论

          本文标题:iOS 【Swift-ObjectMapper对象转换】

          本文链接:https://www.haomeiwen.com/subject/dsilbltx.html