One-Sided Ranges
Express open-ended ranges for slicing.
One-Sided Ranges is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
What Are One-Sided Ranges
A one-sided range omits one bound. It is most often used to slice a collection from or up to a position.
let letters = ["a", "b", "c", "d", "e"]
let tail = letters[2...]
print(Array(tail))From an Index Onward
arr[2...] means "from index 2 to the end". The starting index is included.
let nums = [10, 20, 30, 40]
print(Array(nums[1...]))Up to an Index
arr[..<3] means "from the start up to but not including index 3".
let nums = [10, 20, 30, 40]
print(Array(nums[..<3]))Through an Index
arr[...2] means "from the start through index 2", including index 2.
let nums = [10, 20, 30, 40]
print(Array(nums[...2]))Slices Share Indices
A slice keeps the original indices. The first element of arr[2...] is still at index 2, not 0.
let nums = [10, 20, 30, 40]
let slice = nums[2...]
print(slice.startIndex)
print(slice[2])Converting a Slice
To reindex from 0 or get a standalone array, wrap the slice in Array(...).
let nums = [10, 20, 30, 40]
let rest = Array(nums[1...])
print(rest[0])One-Sided in Pattern Matching
One-sided ranges also work as switch patterns: case 18... matches 18 and above.
let age = 70
switch age {
case ..<18:
print("Minor")
case 18...:
print("Adult")
default:
print("?")
}Lower One-Sided Pattern
case ..<0 matches everything below zero, useful for catching negatives.
let value = -3
switch value {
case ..<0:
print("negative")
case 0:
print("zero")
default:
print("positive")
}Prefix of a String
One-sided ranges slice strings too, when used with string indices. Here we drop the first character via a slice.
let word = "Swift"
let chars = Array(word)
let rest = chars[1...]
print(String(rest))Combining Slices
You can take a head and a tail of an array using two one-sided slices.
let data = [1, 2, 3, 4, 5, 6]
let head = Array(data[..<2])
let tail = Array(data[4...])
print(head)
print(tail)Safe Slicing
Slicing past the bounds crashes, so guard the index first when it comes from input.
let nums = [1, 2, 3]
let start = 1
if start < nums.count {
print(Array(nums[start...]))
}Quick Check
Test one-sided ranges.
Recap: One-Sided Ranges
One-sided ranges like arr[2...], arr[..<3], and arr[...2] slice from or up to a position. Slices keep original indices, so use Array(...) to reindex. They also work as switch patterns.
let a = [1, 2, 3, 4]
print(Array(a[2...]))Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “One-Sided Ranges” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “One-Sided Ranges”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
What will I learn in “One-Sided Ranges”?
Express open-ended ranges for slicing. 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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “One-Sided 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.