Basics: Tuples

Are particularly useful as the return values of functions.

Are useful for temporary groups of related values.

Let http404Error = (404, "Not Found")
// http404Error is of type (Int, String), and equals (404, "Not Found")
Let (statusCode, statusMessage) = http404Error
print("The status code is \(statusCode)")
// Prints "The status code is 404"
print("The status message is \(statusMessage)")
// Prints "The status message is Not Found"
Let (justTheStatusCode, _) = http404Error
print("The status code is \(justTheStatusCode)")
// Prints "The status code is 404"
let http200Status = (statusCode: 200, description: "OK")
Print("The status code is \(http404Error.0)")
// Prints "The status code is 404"
print("The status message is \(http404Error.1)")
// Prints "The status message is Not Found"

Additional information

  • Group multiple values into a single compound value.
  • The values within a tuple can be of any type and don’t have to be of the same type as each other.
  • You can create tuples from any permutation of types, and they can contain as many different types as you like.

Basics: Tuples — Structure map

Clickable & Draggable!

Basics: Tuples — Related pages: