Swift 3.0 Cheat sheet Question

I thought I had a bit of a handle on arrays, but I don’t understand this. Can some one explain what the waldo part is doing at the array.append and why var waldo = array[2]. the (person) comes from that is being printed.

var person1 = “Ray”
var person2 = “Brian”
var array:[String] = [person1, person2]
for person in array {
print(“person: (person)”)
var waldo = array[2]

This clip of code if from the Array section of the Swift 3.0 cheat sheet on this site.

@derekm I don’t understand well what you mean. But let me explain you, so append function meaning is just add to or insert into array. You just put “Waldo” in array, so it’s at 2 index. Array index starts from 0. So in your case in array[0] contains “Ray”, array[1] contains “Brian” and array[2] is “Waldo”. And you just assigned array[2] which is “Waldo” to waldo variable.

@derekm Thanks very much for your question, and my apologies for the delayed response.

To answer your question, arrays start from index 0, and go up by increments of one. The line:


is adding the name “Waldo” to index 2 in the array. (We start at 0, which holds “Ray”, and index 1 holds “Brian”).

The line:

var waldo = array[2] is a bit tricky now :slight_smile:

What it’s doing is pulling the name from index 2 of the array (which is “Waldo”) and assigning it to the variable “waldo”. The confusing part here is that the var waldo is similar to the text contained in the array. There is however one key difference: the reference (i.e. var waldo) has a lowercase “w”, whereas the name contained inside the array (i.e. “Waldo”) has an uppercase “W”. I also wish to point out that even IF the reference variable “waldo” was instead “Waldo” with a capital “W”, the line of code would still work!

The best thing to do in times like this is to play around with the code and see how the compiler reacts to you making changes. :slight_smile:

I hope this helps!

All the best!

Hi @derekm,
There are detailed tutorials on arrays on this site that you can look into. However, here’s a speed course.

var myArray:[String] = []  // This is initialising a blank array
myArray.append("One")      // This adds One as the first element
myArray.append("Two")      // This adds Two as the next element

Since arrays start at index 0, the first element is at index 0, the second at index 1 and so on.

You can also initialise an array with data instead of appending it one by one, such as

var myFilledArray = ["One", Two"]


var value1 = "One"
var value2 = "Two"
var myValueArray:[String] = [value1, value2]

You can also access the array elements referring to them as first and last as

print(myArray.first)   // This would be an optional string
print(myArray.last   // This would be an optional string

similarly you can also access the values and assign it to a variable like,

var myValue1 = MyValueArray[1]    // contains Two, remember index starts as 0
var myValue2 = MyValueArray.first // Contains One

Hope that gives you some context,