Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
¿Cómo mapear el diccionario con claves dinámicas con ObjectMapper?

Soy nuevo en ObjectMapper. Tengo respuesta del servidor:

 { "123123": 10, "435555": 2, "435333": 8, "567567": 4 }

Las claves (dinámicas) van a ser ID. Los valores van a ser COUNT. ¿Cómo puedo mapearlo con ObjectMapper?

Mi código no funciona porque las claves dinámicas:

 extension Item: Mappable { private static let kId = "id" private static let kCount = "count" public init?(map: Map) { self.init() } mutating public func mapping(map: Map) { id <- map[Item.kId] count <- map[Item.kCount] } }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Su respuesta es un objeto y puede acceder a él a través de map.JSON y su tipo es [String: Any] . Entonces puedes usarlo como un Dictionary normal.

Aquí creo una clase llamada Model que tiene una matriz de elementos (de tipo Item ) y en el func mapping(:Map) map.JSON elementos map.JSON a Item .

 class Model: Mappable { typealias Item = (id: String, count: Int) var items: [Item] = [] required init?(map: Map) { } func mapping(map: Map) { let rawDictionary = map.JSON let items = rawDictionary.compactMap { (key, value) -> Item? in guard let intValue = value as? Int else { return nil } return (key, intValue) } self.items = items } } let jsonString = """ { "123123": 10, "435555": 2, "435333": 8, "567567": 4 } """ let model = Model(JSONString: jsonString) print(model?.items[0]) //Optional((id: "123123", count: 10))
over 4 years ago · Santiago Trujillo Report

0

Puedes probar

 do{ let res = try JSONDecoder().decode([String:Int].self, from: data) } catch { print(error) }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!