Ad space (banner)
๐ŸฆSwift Lessons
Lesson 7 / 68

Working with Arrays

In this lesson you will learn the basics of arrays in Swift so you can handle several pieces of data together. This is for anyone searching for "Swift array how to use".

An array is like a shelf of numbered drawers. You create one with [ ] and take items out with their number using enumerated().

The sample code builds an array with let fruits = ["apple", "orange", "grape"] and uses fruits.enumerated() to take the index and the element together while printing them with numbers. The array's type is inferred from its elements, so you get safety without writing the type at all.

A common stumbling block for beginners is needing var rather than let when the array will change. Adding uses append and removing uses remove, but neither can be called on an array declared with let.

In real-world development, arrays are used almost everywhere list data appears, making them Swift's most fundamental data structure.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Swift directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)