Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
Swift Extended iterable unpacking of tuples

We know in Python you can do

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

That is, you can unpack let's say the first element of the tuple and the rest.

I'm struggling to find the same in Swift:

var tuple = ("Boston", "Red Sox", 97, 65, 59.9)
var (first, rest) = tuple
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

There is none (at the moment), Swift is strongly typed. i.e the compiler has to know this information - you can write a function that does the unpacking though.

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

In case you have more than two/three elements in a tuple it is recommended to group them under some model.

over 4 years ago · Santiago Trujillo Denunciar

0

You could make some generic functions to do it:

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))
}

Now you can:

let tuple = ("Boston", "Red Sox", 97, 65, 59.9)
let (first, rest) = firstRest(tuple)
print(first)
print(rest)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda