• Saltar a la navegación principal
  • Saltar al contenido principal

CARLOSZR.COM

Mi blog personal, CarlosZR

  • Blog
  • Contacto
  • Curso de SwiftUI por CarlosZR

Swift and Beach / 09/11/2021

Tuples in Swift

Tuples group multiple values into a single composite value. They can be created on the fly. Values within a tuple can be of any type. They do not have to be the same type as each other. Swift 4 introduces Tuples type.

They are used many times to return multiple values from a function call.

Tuple example:

// Tuple.
var tuple1 = (1, 2, "23")

How to access the value of a tuple

We can access each of the values of a tuple by the number of the position they have within it.

Access the value of a tuple in Swift.
Access the value of a tuple in Swift.

You can name each of the elements of a tuple. Then, you can use the element names to access the values of those elements.

Access an element of a tuple with named elements in Swift
Access an element of a tuple with named elements in Swift

You don’t have to give your tuple elements names if you don’t want to.

When to use a Tuple in Swift

When we want to use exactly certain elements of a certain type, then you should use a Tuple. Tuples in Swift are not recommended for a complex data structure. For more complex data structures we must create a class or a structure.

Specifically, tuples are very useful as return values in functions. Let’s see an example of this.

Function that returns a tuple
Function that returns a tuple

How do I add a tuple to a Swift array?

In this example we are going to see how we can add a tuple to an array. Too easy.

Add tuple to array in swift
Add tuple to array

Tuples Swift example

Let’s see an example of tuples in Swift. Here we can see a tuple that a server returns to us. It is a reply message. It is made up of two elements, a String and an Int, the http status codes.

tuple with status code in swift
Tuple with status code. (Int, String)

You can decompose the content of a tuple into constants or separate variables, then we can access these values as normal.

How to decompose the values of a tuple
How to decompose the values of a tuple

In this example we see how we can access each of the values of a tuple in a single line of code.

And this is all I have to say. If I have helped you, I encourage you to leave me a comment!

Publicado en: Swift and Beach

Interacciones con los lectores

Deja una respuesta Cancelar la respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Carlos ZR

  • Sígueme en Twitter
  • Política de Cookies
  • Política de Privacidad
  • Aviso Legal

Utilizamos cookies para ofrecerte la mejor experiencia en nuestra web.

Puedes aprender más sobre qué cookies utilizamos o desactivarlas en los ajustes.

CARLOSZR.COM
Powered by  GDPR Cookie Compliance
Resumen de privacidad

Esta web utiliza cookies para que podamos ofrecerte la mejor experiencia de usuario posible. La información de las cookies se almacena en tu navegador y realiza funciones tales como reconocerte cuando vuelves a nuestra web o ayudar a nuestro equipo a comprender qué secciones de la web encuentras más interesantes y útiles.

Cookies estrictamente necesarias

Las cookies estrictamente necesarias tiene que activarse siempre para que podamos guardar tus preferencias de ajustes de cookies.

Si desactivas esta cookie no podremos guardar tus preferencias. Esto significa que cada vez que visites esta web tendrás que activar o desactivar las cookies de nuevo.