So far, each variable we've used holds just one piece of data, like a single number or string.
But what if you need to store multiple related items together, like all the player names in a game or items in an inventory?
Engagement Message
How might you group related values?
Swift provides a way to store an ordered collection of items: an array.
You create arrays using square brackets []
, with items separated by commas. For example: [1, 2, 3]
.
Engagement Message
What punctuation separates items inside an array?
Arrays work best when they contain items of the same data type, like all integers or all strings.
Example: [10, 20, 30]
or ["red", "green", "blue"]
Imagine you're tracking your daily steps for a week.
Engagement Message
Would an array be a good way to store the number of steps for each day?
Just like other data types, you assign an array to a variable using the equals sign =
.
Engagement Message
How would you create a variable primes
holding the numbers 2, 3, and 5?
