Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

json - How to map for two different type with ObjectMapper

Im trying to map my data which is come from backend server. But the problem is I have 2 different type one is Int and other ones are String . Im using object mapper for mapping and my code is like ;

class OptionsData : Mappable , ObservableObject {
    
    var id = UUID()
    @Published var name : String?
    @Published var type : String?
    @Published var typeID : String?
    @Published var typeArry : [TypeItem]?
    @Published var image : String?
    @Published var icon : String?
    @Published var platformID : String?
    @Published var imageRatio : Double?
    @Published var priceSell : Double?
    @Published var vat : Double?
    @Published var targetCurrency : String?
    @Published var isDiscountActive : Double?
    @Published var displayVat : Int?
    
    required init?(map: Map) {
        
    }
    
    func mapping(map: Map) {
        
        name <- map["name"]
        type <- map["type"]
        typeID <- map["type_id"]
        typeArry <- map["type_arr"]
        image <- map["image"]
        icon <- map["icon"]
        platformID <- map["platform_id"]
        imageRatio <- map ["image_ratio"]
        priceSell <- map ["price_sell"]
        targetCurrency <- map["target_currency"]
        vat <- map ["vat"]
        isDiscountActive <- map ["is_discount_active"]
        displayVat <- map ["display_vat"]
        
        
    }
    
    init(name : String? = nil , type : String? = nil , typeID : String? = nil , typeArry : [TypeItem]? = nil , image : String? = nil ,icon : String? = nil , platformID : String? = nil ,image_ratio : Double? = nil,price_sell : Double? = nil,vat : Double? = nil ,targetCurrency : String? = nil,isDiscountActive : Double? = nil,displayVat : Int? = nil) {
        
        self.name = name
        self.type = type
        self.typeID = typeID
        self.typeArry = typeArry
        self.image = image
        self.icon = icon
        self.platformID = platformID
        self.imageRatio = image_ratio
        self.priceSell = price_sell
        self.vat = vat
        self.targetCurrency = targetCurrency
        self.isDiscountActive = isDiscountActive
        self.displayVat = displayVat
        
    }
}

im trying to do this for typeID . is there is a way to work for 2 different type ?

question from:https://stackoverflow.com/questions/65935802/how-to-map-for-two-different-type-with-objectmapper

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...