Join John Nastos for an in-depth discussion in this video Understanding arrays, part of Learn Swift 2: The Basics.
- The concept of an array is an important one in any programming language. It's the most basic way of storing a collection of individual items. For example, let's say you had four different numbers and you wanted to store them using one variable. You would use an array. The first way to define an array in Swift is with a literal definition using square brackets to group together a collection of items separated by commas. For example, in the line var myArray = [3,5,7] myArray is a three-item array that holds the integer values 3, 5, and 7.
Because it's defined as a variable and not a constant, it can be modified. Items can be removed, added, altered, etc. Notice that the compiler does some type inference here to figure out that it's an array of integers. If I wanted to explicitly set the type, I could write var myArray : [Int] = [3,5,7] Notice that here, the Int is also surrounded by square brackets telling the compiler that it's an array of integers and not just a single one.
I can then get information out of the array by again using the square brackets, this time telling the compiler which item I want back. Swift arrays are zero-based, meaning the first item is at index zero. So if I want to get the number in the first space, I would say myArray and in the results column on the right you can see that it returned the first value in the array. If I wanted the last item in the array, which right now has three items, I could ask for myArray, not , because remember, the first slot is number 0, not 1.
If the array is mutable, I can also assign values using square brackets. For example, I can say myArray = 10 Note that in order to do this, the index has to already exist. For example, I can't say myArray = 10 because myArray doesn't have that many slots. An array can also be defined using an initializer.
This could be useful, for example, to define an empty array that can be added to later. I could write var myArray2 and make sure that it's typed as an [Int] = Array() Then later, when I wanted to add an item to the array, I could do it by calling the append method on it. So, later in my code, I could add the line myArray2.append and add the number 5 to the array.
Then, as you can see in the results column, the array is one item long containing the number 5 in the first spot. Notice that in order to do this I had to define the array as an Int array. That's because when I created the initializer, it didn't have a specific type associated with it, and the compiler needs to know what type that is. Then later, when I added the number, the compiler responded correctly because it was expecting an integer. Let's say that you wanted an array that not only contained numbers but also strings, something where say the first two slots held integer values and then the third and fourth contained strings.
Obviously, you cannot do that by creating an array of the type Int or string since it has both. Luckily, Swift includes a special keyword called Any. The Any type allows us to store anything in it while specifically warning the compiler that this is the case. So I can define my array as var complexArray of type [Any] = Array() Then, I can add integers, strings, or anything else to it.
If you want to get technical with this concept, you can Command + click on the Any keyword in Xcode and get taken to the definition. Any is defined as a protocol that all types will inherently conform to. Let's cover a couple of other basic array tasks. One common piece of information that's useful to know about an array that maybe you got back from a function call and didn't create yourself is how many items are in that array. In Swift, you can get this information by getting the count property.
So I can say myArray, which was the first array that I had created, .count. And in the results column, you can see that it returns 3, the number of items in the array. Earlier, I covered adding items to an array using the append method. But, what if you wanted to remove an item instead? This is done using the removeAtIndex method. I have to tell it which item to remove. In this case, I'll remove the first item in the array at index zero.
Then, that item gets removed and the array has one fewer item in it. Now, in the results column, you can see that when I called the removeAtIndex method, 10 was returned, which was the number that got removed. And in the next line, you can see that the results column shows that myArray now just contains two items: 5 and 7.
First, learn how to install Xcode, the IDE designed for Apple developers, and use Swift playgrounds, an innovative coding environment that displays results as you code. Author John Nastos then dives deep into Swift syntax: the variables and constants, data types, arrays, loops, classes, and functions that make your apps run. Next, discover how to build an engaging app interface with storyboards and Interface Builder, connect the interface and code, and use table views to present lists. John shows how to store and retrieve data, and interact with Objective-C, the original app development language that still integrates beautifully with Swift. Using the exercise files provided with the course, you'll assemble a complete working app with Swift while gaining the skills to go create the next one on your own.
- Downloading and installing Xcode
- Using Swift playgrounds
- Understanding Swift syntax
- Debugging Swift code
- Controlling flow with expressions, conditional statements, and loops
- Using and writing Swift functions
- Creating classes, structs, and enums
- Using storyboards in Interface Builder
- Building an app with table views
- Storing and retrieving data
- Interacting with Objective-C