Difference between Tuples and Arrays in swift

  • Both tuples and arrays allow us to hold several values in one variable, but tuples hold a fixed set of things that can’t be changed, whereas variable arrays can have items added to them indefinitely
  • Another advantage to tuples is that each value is specifically created by you, so as well as providing a name you also provide a type. So, you could create a tuple like this one:

var person = (name: “Paul”, age: 40, isMarried: true)

That combines a string, an integer, and a Boolean in a single value, which would be pretty ugly in an array.’

EXTRA POINTS ON TUPLES:

  • You can access items in a tuple using numerical positions or by naming them, but Swift won’t let you read numbers or names that don’t exist
  • Tuples may have duplicate values
  • Tuples store values together in a single value
  • Tuples are perfectly capable of holding multi-line strings