In this demo, you’ll write Swift code to calculate the first few digits in the Fibonacci sequence and store them in an array.
Remove the existing code in a new playground and create a new Int array variable to store the values of the Fibonacci sequence:
var fibonacciSeries: [Int] = []
Using an initial value of [] here means it’s an empty array.
Create a constant for the first element in the sequence and then append it to the array:
let fibonacci_0 = 0
fibonacciSeries.append(fibonacci_0)
The underscore-zero in the variable doesn’t mean anything except to you. The more descriptive variable names you use, the easier it will be to read this code in a few weeks - I promise you that. You can always use letters or short names for variables like x, y, s1, s2, … the problem with those names is if you use many of them and your code is doing something a little complex, the moment you switch your focus to sip from your cup of coffee, you’ll most probably lose track of what some of the variables on your screen were and you’ll backtrack a few steps reading the code. Because Swift is a compiled language, you don’t get code that runs any faster or takes up less space by using short variable names. Be descriptive.
Now, your array has only one element.
If you try to print the element at index 1, the playground crashes when you run it:
print(fibonacciSeries[1])
In the bottom pane where the console messages appear, Xcode says there is a fatal error where the index is out of range.
Remember that arrays always start with index zero and the element at index one is actually the second element, not the first.
Comment this print line with slashes or delete it to remove the crash.
Now, create the second element in the sequence and add it to the array:
let fibonacci_1 = 1
fibonacciSeries.append(fibonacci_1)
It’s like the first element: The name of the variable represents its index with underscore one, and you added it to the array.
Next, define the third element, which is the sum of the first and second elements:
let fibonacci_2 = fibonacci_0 + fibonacci_1
fibonacciSeries.append(fibonacci_2)
Repeat the same for the fourth and fifth elements:
let fibonacci_3 = fibonacci_1 + fibonacci_2
fibonacciSeries.append(fibonacci_3)
let fibonacci_4 = fibonacci_2 + fibonacci_3
fibonacciSeries.append(fibonacci_4)
Run the playground and notice the values in the right pane of the window.
Beside each line with append, the pane shows the array’s state after appending the element. Hover the cursor over it and click the small eye icon. It’ll show more details, including the elements at each index in the array.
Print all the elements in the array one at a time. Add these print statements:
print(fibonacciSeries[0])
print(fibonacciSeries[1])
print(fibonacciSeries[2])
print(fibonacciSeries[3])
print(fibonacciSeries[4])
Run the playground. Notice the numbers in the bottom pane. You can also print the whole array to see everything inside it. Add this print statement:
print(fibonacciSeries)
Run the playground. You see the whole array is printed between square brackets and separated by commas, just like you would enter it if you set the initial value of an array variable.
Remember that this array has five elements from the index zero to the index four. If you try accessing the index five, the playground crashes just like before.
As you read through the code, notice that the elements starting from the third are written almost the same — the only difference is the numbers after the underscore. Another thing to note is that you didn’t access the values from the array at all.
If only there was a way you could write the code once and have it repeat itself for each element in the sequence, like when you add the element in index five to append the sum of elements in index four plus index three. That way, your code would look much more organized and you’d use the array properly.
Luckily, this is what you’ll cover in the next lesson.