Skip to content
Learn Netverks

Lesson

Step 13/36 36% through track

arrays-swift

Arrays

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Arrays: the syntax, patterns, and safety habits you need before advancing in Swift.

Teams still ship Arrays in Swift codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Arrays in contexts like: iPhone/iPad/Mac apps, server-side Swift (niche), and Apple toolchain projects.

Write Swift in main.swift with print(), click Run on server—the dev runner swiftc compiles and runs the binary (requires Swift toolchain, typically macOS; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Array stores ordered, zero-indexed collections with value semantics—mutating a copied array does not affect the original when elements are value types.

Creating and mutating

var nums = [1, 2, 3]
nums.append(4)
nums.insert(0, at: 0)

let doubled = nums.map { $0 * 2 }

Common operations

  • count, isEmpty, first, last
  • filter, map, reduce, sorted()
  • Subscript: nums[0]—bounds-checked at runtime

Important interview questions and answers

  1. Q: Array vs NSArray?
    A: Swift Array bridges to Foundation; prefer typed Swift arrays in new code.
  2. Q: Copy-on-write?
    A: Arrays share storage until mutated—efficient passing without eager deep copies.

Self-check

  1. How do you append to a var array?
  2. What happens if you subscript out of bounds?

Tip: Arrays use copy-on-write—efficient until one copy mutates shared storage.

Interview prep

Copy-on-write?

Arrays share storage until mutated—efficient passing.

Out of bounds?

Runtime trap on invalid subscript.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Array vs NSArray?
  • Copy-on-write?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump