Tuples
group multiple values into a single compound value. The values within a tuple can be of any type and do not have to be of the same type as each other.
In this example, (404, "Not Found") is a tuple that describes an
HTTP status code
. An HTTP status code is a special value returned by a web server whenever you request a
web page. A status code of 404 Not Found is returned if you request a webpage that doesn’t exist.
1 let http404Error = (404, "Not Found")
2 // http404Error is of type (Int, String), and equals (404, "Not Found")
The (404, "Not Found") tuple groups together an Int and a String to give the HTTP status code two separate values: a number and a human-readable description. It can be
described as “a tuple of type (Int, String)”.
You can create tuples from any permutation of types, and they can contain as many different types as you like. There’s nothing stopping you from having a tuple of
type (Int, Int, Int), or (String, Bool), or indeed any other permutation you require.
You can
decompose
a tuple’s contents into separate constants or variables, which you then access as usual:
1 let (statusCode, statusMessage) = http404Error
2 println("The status code is \(statusCode)")
3 // prints "The status code is 404"
4 println("The status message is \(statusMessage)")
5 // prints "The status message is Not Found"
If you only need some of the tuple’s values, ignore parts of the tuple with an underscore (_) when you decompose the tuple:
1 let (justTheStatusCode, _) = http404Error
2 println("The status code is \(justTheStatusCode)")
3 // prints "The status code is 404"
Alternatively, access the individual element values in a tuple using index numbers starting at zero:
1 println("The status code is \(http404Error.0)")
2 // prints "The status code is 404"
3 println("The status message is \(http404Error.1)")
4 // prints "The status message is Not Found"
You can name the individual elements in a tuple when the tuple is defined:
let http200Status = (statusCode: 200, description: "OK")
If you name the elements in a tuple, you can use the element names to access the values of those elements:
1 println("The status code is \(http200Status.statusCode)")
2 // prints "The status code is 200"
3 println("The status message is \(http200Status.description)")
4 // prints "The status message is OK"
Tuples are particularly useful as the return values of functions. A function that tries to retrieve a web page might return the (Int, String) tuple type to describe the
success or failure of the page retrieval. By returning a tuple with two distinct values, each of a different type, the function provides more useful information about
its outcome than if it could only return a single value of a single type. For more information, see Functions with Multiple Return Values.
N O T E
Tuples are useful for temporary groups of related values. They are not suited to the creation of complex data structures. If your data structure is likely to persist beyond a
temporary scope, model it as a class or structure, rather than as a tuple. For more information, see Classes and Structures.
OptionalsOptionals
You use
optionals
in situations where a value may be absent. An optional says:
or
N O T E
The concept of optionals doesn’t exist in C or Objective-C. The nearest thing in Objective-C is the ability to return nil from a method that would otherwise return an object,
with nil meaning “the absence of a valid object.” However, this only works for objects—it doesn’t work for structs, basic C types, or enumeration values. For these types,
Objective-C methods typically return a special value (such as NSNotFound) to indicate the absence of a value. This approach assumes that the method’s caller knows there is a
special value to test against and remembers to check for it. Swift’s optionals let you indicate the absence of a value for
any type at all
, without the need for special
constants.
Here’s an example. Swift’s String type has a method called toInt, which tries to convert a String value into an Int value. However, not every string can be converted
There
is
a value, and it equals
x
•
There
isn’t
a value at all•