Creating Slices with Ranges
Extract subsequences without copying.
Creating Slices with Ranges is a free Swift Academy lesson on CoddyKit — lesson 1 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.
Subscripting with a Range
When you subscript an array with a range, Swift does not copy elements into a new array. Instead it returns an ArraySlice that shares storage with the original array.
let numbers = [10, 20, 30, 40, 50]
let slice = numbers[1..<4]
print(slice)
print(type(of: slice))Half-Open Range
The operator ..< creates a half-open range. 1..<4 includes indices 1, 2, and 3 but not 4.
let letters = ["a", "b", "c", "d", "e"]
let middle = letters[1..<4]
print(middle)Closed Range Slicing
Use ... for a closed range when you want the upper bound included. 1...3 covers indices 1, 2, and 3.
let scores = [88, 92, 75, 64, 99]
let closed = scores[1...3]
print(closed)One-Sided Ranges
Swift supports one-sided ranges. arr[2...] means from index 2 to the end, and arr[..<2] means everything before index 2.
let data = [1, 2, 3, 4, 5, 6]
print(data[3...])
print(data[..<3])Slices Are Views
An ArraySlice is a view into the parent array. It is cheap to create because no elements are copied until you mutate or convert it.
let big = Array(1...1000)
let window = big[100..<105]
print(window)Iterating Over a Slice
You can loop over a slice just like an array. The values are the same elements the parent array holds.
let words = ["sun", "moon", "star", "comet"]
let pair = words[1..<3]
for w in pair {
print(w)
}Slice of Strings
Slicing works with any element type. Here we take a range out of an array of names.
let names = ["Ada", "Bob", "Cleo", "Dan", "Eve"]
let some = names[0..<2]
print(some)Combining With count
You can build a range dynamically using the array's count to grab the last few elements.
let nums = [5, 10, 15, 20, 25]
let lastTwo = nums[(nums.count - 2)..<nums.count]
print(lastTwo)Empty Slice
A range whose lower and upper bounds are equal produces an empty slice. This is valid and safe.
let arr = [1, 2, 3, 4]
let empty = arr[2..<2]
print(empty)
print(empty.isEmpty)Slicing and Summing
Because a slice still conforms to the same protocols, you can call methods like reduce directly on it.
let values = [2, 4, 6, 8, 10]
let sum = values[1..<4].reduce(0, +)
print(sum)Why Slices Matter
Slices let you work with sub-sections of large arrays without paying the cost of copying. This is great for algorithms like binary search or sliding windows.
let series = Array(1...20)
let firstHalf = series[..<10]
let secondHalf = series[10...]
print(firstHalf.count, secondHalf.count)Quick Check
Test your understanding of range slicing.
Recap
You learned that subscripting an array with a range returns an ArraySlice that shares storage with the parent. Half-open ..< excludes the upper bound, closed ... includes it, and one-sided ranges grab a prefix or suffix cheaply.
let r = [10, 20, 30, 40]
print(r[1..<3])
print(r[1...])
print(r[..<2])Frequently asked questions
Is the “Creating Slices with Ranges” lesson free?
Yes — the full text of “Creating Slices with Ranges” 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 “Creating Slices with Ranges”?
Extract subsequences without copying. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Creating Slices with Ranges” 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
- Creating Slices with Ranges
- ArraySlice Index Semantics
- Converting Slices Back to Arrays
- prefix, suffix, and dropFirst