Swift - Data Type Tuples

Introduction

A tuple is a collection of data.

Tuples bundle any number of values of any type together into a single value.

You create them by wrapping comma-separated values inside parentheses:

let fileNotFound = (404,"File Not Found") 

You can get values out of it by index:

fileNotFound.0 // 404 

A tuple's indexes start at 0.

You can apply labels to values inside tuples:

let serverError = (code:500, message:"Internal Server Error") 
serverError.message // "Internal Server Error" 

You can use tuple to return multiple values from a function.

Related Topics