How to create range in Swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Ranges are one of the most common small language features in Swift because they show up in loops, slicing, pattern matching, and collection APIs. The important part is not just memorizing the operators, but understanding which bounds are included and which APIs expect a closed, half-open, or one-sided range.
Closed and Half-Open Ranges
Swift has two standard range operators for bounded ranges.
A closed range includes both endpoints:
A half-open range includes the lower bound and excludes the upper bound:
This distinction matters in loops. A half-open range is especially useful with counts and indices because the upper bound is often the element count.
That prints 0, 1, and 2.
Create One-Sided Ranges
Swift also supports one-sided ranges. These are often used for slicing collections rather than standalone numeric iteration.
Examples:
The semantics are:
- '
..<nmeans everything beforen' - '
...nmeans everything throughn' - '
n...means everything fromnonward'
These are especially convenient when you do not want to calculate both bounds manually.
Use Ranges with Arrays and Strings Carefully
Ranges are heavily used for collection slicing, but not every collection behaves like an integer-indexed array.
For arrays, integer indices work directly:
Strings are different because Swift strings are collections of grapheme clusters, not random-access arrays of bytes. That means you use String.Index rather than integer offsets.
That is a range too, but the bounds are string indices rather than plain integers.
Create a Range Value Explicitly
You can store a range in a variable and pass it around just like other values.
This can be useful when a function should accept a range parameter:
Use stride When You Need Steps
A normal range does not let you specify a step size. For that, use stride.
If you want the end included when possible, use through instead of to:
This is an important distinction because many developers initially expect 0...10 to have a built-in step parameter. Swift separates range creation from stepped iteration.
Ranges in Conditions and Pattern Matching
Ranges also work in switch cases and boolean membership checks.
This is a clean way to express intervals without chaining multiple comparisons.
Common Pitfalls
The most common mistake is confusing ... with ..< and accidentally including or excluding the upper bound. Another is using integer offsets directly with strings, which does not match Swift’s string indexing model. Developers also sometimes try to use a range when they really need a stepped sequence, which means stride is the correct tool. A final issue is forgetting that array slices are views over a range of the original collection, not automatically brand-new arrays unless explicitly converted.
Summary
- Use
a...bfor a closed range anda..<bfor a half-open range. - One-sided ranges are useful for slicing collections.
- Arrays use integer indices, but strings use
String.Indexvalues. - Use
stridewhen you need stepped iteration rather than a plain range. - Ranges are useful beyond loops, including slicing, validation, and pattern matching.
Related reading
- How to create RecyclerView with multiple view types
- How to create RecyclerView with multiple view types
- How to create ripple effect in simple layout
- How to create UICollectionViewCell programmatically
- How to create UILabel programmatically using Swift?
- How to create UILabel programmatically using Swift?
- How to crop UIImage on oval shape or circle shape?
- How to customize the background color of a UITableViewCell?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.