Thuta Learning
BasicProgrammingbeginner

Arrays

Relax. We'll talk through this in plain words — no textbook voice.

Array is a collection that stores data of the same type in order. You can use arrays to store things like product lists, menu items, todo tasks, or lesson titles. Array indexes start at 0.

swift
var lessons = ["Intro", "Variables", "Arrays"]

print(lessons[0])

lessons.append("Functions")

for lesson in lessons {
    print("Lesson: \(lesson)")
}

print("Total lessons: \(lessons.count)")

lessons[0] grabs the very first item. append() adds a new item, and a for-in loop walks through each item in the array.

You should see
Intro Lesson: Intro Lesson: Variables Lesson: Arrays Lesson: Functions Total lessons: 4

Easy traps

  • If an array only has 3 items, lessons[3] is actually the fourth item. lessons[4] would go out of range.
Arrays | Thuta Learning