0Pricing
Swift Academy · Lesson

ArraySlice Index Semantics

Understand why slice indices keep original positions.

ArraySlice Index Semantics is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Slices Keep Original Indices

A key surprise: an ArraySlice does not reindex from zero. It keeps the same indices it had in the parent array.

let arr = [10, 20, 30, 40, 50]
let slice = arr[2..<5]
print(slice.startIndex)
print(slice.endIndex)

startIndex Is Not Always Zero

The startIndex of a slice equals the lower bound used to create it. For arr[2..<5] the start index is 2.

let arr = [1, 2, 3, 4, 5, 6]
let slice = arr[3..<6]
print("start:", slice.startIndex)
print("first value:", slice[slice.startIndex])

Subscripting a Slice

Because indices are preserved, you must use the slice's own indices. Accessing slice[0] on a slice that starts at index 2 will crash at runtime.

let arr = [100, 200, 300, 400]
let slice = arr[2..<4]
print(slice[2])
print(slice[3])

Use startIndex Safely

To get the first element regardless of where the slice starts, use slice.first or slice[slice.startIndex].

let arr = [5, 6, 7, 8, 9]
let slice = arr[2..<5]
print(slice.first ?? -1)
print(slice[slice.startIndex])

endIndex Is One Past the Last

Like arrays, endIndex points one position past the last valid element. Subscripting at endIndex is invalid.

let arr = [11, 22, 33, 44]
let slice = arr[1..<3]
print("endIndex:", slice.endIndex)
print("last valid:", slice[slice.endIndex - 1])

Counting Vs Indexing

The count of a slice is the number of elements, which can differ from its endIndex. Never assume count equals the last index.

let arr = [0, 1, 2, 3, 4, 5]
let slice = arr[3..<6]
print("count:", slice.count)
print("startIndex:", slice.startIndex)

Indices Property

Use the indices property to iterate over the valid index range of a slice safely.

let arr = [10, 20, 30, 40, 50]
let slice = arr[1..<4]
for i in slice.indices {
    print(i, slice[i])
}

Why Preserve Indices

Preserving indices lets you map a position in the slice back to the original array without bookkeeping. The index you find is valid in the parent too.

let arr = ["a", "b", "c", "d", "e"]
let slice = arr[2..<5]
if let idx = slice.firstIndex(of: "d") {
    print("found at parent index:", idx)
    print("parent has:", arr[idx])
}

Enumerate Resets to Zero

If you want zero-based positions, use enumerated(). It yields offsets starting at 0 regardless of the slice's startIndex.

let arr = [9, 8, 7, 6, 5]
let slice = arr[2..<5]
for (offset, value) in slice.enumerated() {
    print(offset, value)
}

Distance From Start

To compute a zero-based offset from an index, use distance(from:to:).

let arr = [1, 2, 3, 4, 5]
let slice = arr[2..<5]
let idx = slice.startIndex + 1
let offset = slice.distance(from: slice.startIndex, to: idx)
print("offset:", offset, "value:", slice[idx])

A Common Bug

Forgetting that slices keep parent indices is one of the most common Swift bugs. Always prefer first, enumerated(), or startIndex over a hardcoded 0.

let arr = [100, 200, 300, 400, 500]
let slice = arr[3..<5]
// Safe: use first, not slice[0]
print(slice.first ?? 0)

Quick Check

Reason about slice indices.

Recap

An ArraySlice keeps the parent's indices: its startIndex is the lower bound, not zero. Use first, startIndex, indices, or enumerated() for safe access, never a hardcoded 0.

let slice = [5, 6, 7, 8, 9][3..<5]
print(slice.startIndex, slice.first ?? -1)

Frequently asked questions

Is the “ArraySlice Index Semantics” lesson free?

Yes — the full text of “ArraySlice Index Semantics” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “ArraySlice Index Semantics”?

Understand why slice indices keep original positions. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “ArraySlice Index Semantics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating Slices with Ranges
  2. ArraySlice Index Semantics
  3. Converting Slices Back to Arrays
  4. prefix, suffix, and dropFirst
← Back to Swift Academy