Swift Basics - Tuples

You can use tuples to group multiple values of different types into a single value.
var food = ("Ice cream", 'Strawberry", 3, 150)
You can give each a name:
var (dish, flavour, scoops, calories) = food

To get an item from the tuple:
var dish = food.0
var flavour = food.1

You can also create a tuple with a key:name association:
var food = (dish:"Ice cream", flavour:"Strawberry", scoop:3, calories:150.2)

You can use tuples in place of classes and objects that do not have any methods.

Comments

Popular posts from this blog

Setting up a playground

Go to another page