Tuples: One Fish, Two Fish, String, Double?


let tupleName = (val1,val2...)

Tuples allow you to define value objects that combine more than one value. Not only that, but the values don't even need to be of the same type. For instance you can create a Tuple that combines a String and an Int, a Double and Bool, etc.

Tuples also allow definition of keys for the values (similar to Dictionaries). Multiple value objects can be declared at once when a Tuple is referenced (see examples below). Simliar to Arrays, Tuple values can be referenced by their index (position) in the Tuple (see example below). For Tuples that have keys defined, the values can be referenced by their key.

Simple Tuple with String and int

let employee = ("Joe Smith",12345)

// reference Tuple value by index
println("Employee Name: \(employee.0)")
println("Employee Number: \(employee.1)")

Tuple defined with keys

let employee = (employeeName: "Joe Smith", employeeNumber: 12345)

// reference Tuple value by key
println("Employee Name: \(employee.employeeName)")
println("Employee Number: \(employee.employeeNumber)")

Multiple value objects defined when referencing an existing Tuple

let (empName, empNumber) = employee

// reference Tuple value by new value object name
println("Employee Name: \(empName)")
println("Employee Number: \(empNumber)")