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

162
Views
Swift Desempaquetado iterable extendido de tuplas

Sabemos que en Python puedes hacer

 s = (3,5,2) a, *b = s # a=3 b=[5,2]

Es decir, puede desempaquetar digamos el primer elemento de la tupla y el resto .

Estoy luchando por encontrar lo mismo en Swift:

 var tuple = ("Boston", "Red Sox", 97, 65, 59.9) var (first, rest) = tuple
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

No hay ninguno (por el momento), Swift está fuertemente tipado. es decir, el compilador tiene que conocer esta información; sin embargo, puede escribir una función que haga el desempaquetado.

 func unpack(tuple: (String, String, Double, Double, Double)) -> (String, (String, Double, Double, Double)) { return (tuple.0,(tuple.1, tuple.2, tuple.3, tuple.4)) }

En caso de tener más de dos/tres elementos en una tupla se recomienda agruparlos bajo algún modelo.

over 4 years ago · Santiago Trujillo Report

0

Podrías hacer algunas funciones genéricas para hacerlo:

 func firstRest<A, B, C>(_ tup: (A, B, C)) -> (A, (B, C)) { (tup.0, (tup.1, tup.2)) } func firstRest<A, B, C, D>(_ tup: (A, B, C, D)) -> (A, (B, C, D)) { (tup.0, (tup.1, tup.2, tup.3)) } func firstRest<A, B, C, D, E>(_ tup: (A, B, C, D, E)) -> (A, (B, C, D, E)) { (tup.0, (tup.1, tup.2, tup.3, tup.4)) } func firstRest<A, B, C, D, E, F>(_ tup: (A, B, C, D, E, F)) -> (A, (B, C, D, E, F)) { (tup.0, (tup.1, tup.2, tup.3, tup.4, tup.5)) }

Ahora usted puede:

 let tuple = ("Boston", "Red Sox", 97, 65, 59.9) let (first, rest) = firstRest(tuple) print(first) print(rest)
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!