Arrays in Swift are ordered lists that you can use to store items, or elements, in. In this video, learn about array syntax and create your first array.
- [Instructor] We're going to start our exploration of collection types. So open up the AdventureSwift Working folder and go to chapter four Working with collections playground. Collection types are used when you need to store groups or as the name suggests, collections of values which are also referred to as elements. Now arrays work like ordered lists, meaning that each element or value is organized by the order it was added and each element has a corresponding index value.
Arrays are also zero indexed so the first item is stored at index zero, sort of like what we saw with string indexes. Like we've seen before, there are several ways to initialize empty variables. So let's start by creating a variable called empty array and we're going to give this one a type annotation, and the way we do that is using the array key word, left arrow, right arrow. Now inside the arrows, we need to explicitly say what kind of item types this array can hold.
So in this case, we'll just say it's an array of strings equals open and closed brackets. So that's the first way. Let's create another empty array label called emptyArray2. And we'll just set this directly to array of type string and open and close parentheses to initialize empty array. Again, another empty array, this time we're just going to use shorthand which is open and closed brackets, and I'm going to say it can hold doubles and an open and closed parentheses to initialize it.
The last way we can create an empty array is var emptyArray4 and then a type annotation in shorthand, and we'll say this is an array of type int and equals open and closed brackets. All of these empty arrays are equal to the compiler as you can see on the right hand side of the inspector, they're all empty. So choose the one that best suits your style of programming. There's no benefit or downside to any of these. Now let's create an actual array with items in it.
So var and I'm going to call this levelDifficulties, and I'm going to use this shorthand here and I'm just going to fill it with a few values. So easy, moderate, veteran, and nightmare. We can make use of type inference with arrays as well as all the other data types we've explored so far. So levelDifficulties is inferred to be an array of type string.
We can also use some of the dot notation that we used earlier with strings. So levelDifficulties dot count and levelDifficulties that is empty. Since arrays work off indexes starting with zero, we can access each value or arrange of values by using something called subscript syntax. So let's say we wanted to access the first item in levelDifficulties. Let's create a new variable to hold this called easyDifficulty and set this equal to levelDifficulties open and closed brackets and inside we're going to use index of zero.
Now the brackets and the index are the subscript syntax. The last thing we want to do is see if we can loop through all our array values and print them out. We can use a foreign loop for this even though we haven't covered loops or control flow yet. So we'll say for difficulty in levelDifficulties, open and closed brackets, and here we're just going to print out each difficulty.
Just to note, we can also grab the index for each difficulty as we loop through an array. So we'll also use a for loop for this, for every index and difficulty in levelDifficulties. So so far we're capturing each index and each difficulty for every item. Now in order to this we actually need to access a property of levelDifficulties called enumerated. Now this is going to return a sequence of pairs.
The first value is going to be the index and the second value is going to be the actual element. So that's why here at the beginning of the for loop, we're capturing index and difficulty. Now we're just going to print out a string and we're going to use a little string interpolation. Say we're going to print out index and we're going to print out each difficulty. And there we go.
- Starting new playgrounds and projects
- Variables and constants
- Writing single and multiline comments
- Core string methods
- Working with numbers
- Working with collections
- Creating arrays
- Working with sets
- Application control flow
- Writing functions
- Basic Swift classes and structs