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

255
Views
Reducir `[URLQueryItem]` a `[String: Any]`

Actualmente tengo esta función de reduce gruesa ...

 blah: [String: Any] = queryItems.reduce([String: Any]()) { (params: [String: Any], queryItem: URLQueryItem) in var output = params output[queryItem.name] = queryItem.value return output }

Estoy seguro de que hay una manera mucho más simple de hacer esto, pero no puedo entender cómo funcionaría.

¿Hay una mejor manera de hacer esto?

Por "mejor" me refiero a más limpio, más corto, más elegante, etc...

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Es posible usar reduce(into:_:) en lugar de reduce(_:_) . Esto le ahorra las líneas y la sobrecarga de copiar params para cada iteración:

 let blah: [String: Any] = (urlComponents.queryItems ?? []).reduce(into: [:]) { params, queryItem in params[queryItem.name] = queryItem.value }

Se prefiere este método sobre reduce(_:_:) por eficiencia cuando el resultado es un tipo de copia en escritura, por ejemplo, una matriz o un diccionario.

over 4 years ago · Santiago Trujillo Report

0

Puede crear un diccionario a partir del nombre y el valor de cada elemento de consulta con

 let items = urlComponents.queryItems ?? [] let dict = Dictionary(items.lazy.map { ($0.name, $0.value as Any) }, uniquingKeysWith: { $1 })

En el caso de un nombre duplicado, el último valor gana (esto se puede controlar con los parámetros uniquingKeysWith: .

O elimine el as Any cast para obtener un diccionario de tipo [String: String?] :

 let items = urlComponents.queryItems ?? [] let dict = Dictionary(items.lazy.map { ($0.name, $0.value ) }, uniquingKeysWith: { $1 })

Alternativamente

 let items = urlComponents.queryItems ?? [] let dict = Dictionary(items.lazy.map { ($0.name, [$0.value] ) }, uniquingKeysWith: +)

para construir un diccionario de tipo [String : [String?]] , que contenga todos los valores para cada nombre.

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!