iOS Swift Collections and Control Flow Control Flow With Loops While and Repeat While

Subscripts?

What is a subscript? We haven't learned about those and he uses it in "print(todo[index])" Please help!

2 Answers

Alex Koumparos
Alex Koumparos
Python Web Development Techdegree Student 35,893 Points

Subscript is the term used to describe the value in square brackets when using that to access a collection like Array or Dictionary. For example, in the array myArray = [1, 2, 3, 4] we would access the 0-th element (1) by writing myArray[0]. The [0] is the subscript.

Similarly, in a dictionary, such as myDictionary = ["a" : "apple", "b" : "banana"] we could access "banana" using myDictionary["b"] where the key (here "b") in square brackets is the subscript.

Hope that helps.

So for example, if number = 3 and I do print(randomArray[number]) I would get the fourth value in the array?

thanks!

Alex Koumparos
Alex Koumparos
Python Web Development Techdegree Student 35,893 Points

Exactly (unless you have fewer than four elements in the array, in which case you'd get an out-of-bounds error)