Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This
This chapter is from the book

Tuples

Using tuples (pronounced “TWO-pulls” or “TUH-pulls”) is a way to group multiple values into one value. Think of associated values. Here is an example with URL settings:

let purchaseEndpoint = ("buy","POST","/buy/")

This tuple has a String, a String, and a String. This tuple is considered to be of type (String, String, String). You can put as many values as you want in a tuple, but you should use them for what they are meant for and not use them like an array or a dictionary. You can mix types in tuples as well, like this:

let purchaseEndpoint = ("buy","POST","/buy/",true)

This tuple has a String, a String, a String, and a Bool. You are mixing types here, and this tuple is considered to be of type (String, String, String, Bool). You can access this tuple by using its indexes:

purchaseEndpoint.1 // "POST"
purchaseEndpoint.2 // "/buy/"

This works well but there are some inconveniences here. You can guess what POST and /buy/ are, but what does true stand for? Also, using indexes to access the tuple is not very pretty or descriptive. You need to be able to be more expressive with the tuple.

You can take advantage of Swift’s capability to name individual elements to make your intentions clearer:

let purchaseEndpoint = (name: "buy", httpMethod: "POST",URL: "/buy/",useAuth: true)

This tuple has String, String, String, and Bool (true or false) values, so it is the same type as the previous tuple. However, now you can access the elements in a much more convenient and descriptive way:

purchaseEndpoint.httpMethod = "POST"

This is much better. It makes much more sense and reads like English.

You can decompose this tuple into multiple variables at once. Meaning you can take the tuple and make multiple constants or variables out of it in one fell swoop. So if you want to get the name, the httpMethod, and the URL into individual variables or constants, you can do so like this:

let (purchaseName, purchaseMethod, purchaseURL, _) = purchaseEndpoint

Here, you are able to take three variables and grab the meat out of the tuple and assign it right to those variables. You use an underscore to say that you don’t need the fourth element out of the tuple. Only three out of the four properties of the tuple will be assigned to constants.

In Chapter 3, “Making Things Happen: Functions,” you will use tuples to give functions multiple return values. Imagine having a function that returned a tuple instead of a string. You could then return all the data at once and do something like this:

func getEndpoint(endpoint:String) ->
    (description: String, method: String, URL: String) {
    return (description: endpoint, method: "POST", URL: "/\(endpoint)/")
}
let purchaseEndpoint = getEndpoint("buy")
print("You can access the
    \(purchaseEndpoint.description) endpoint at the URL \(purchaseEndpoint.URL)")
  • + Share This
  • 🔖 Save To Your Account