Swift Ranges
In Swift, a range is a series of values between two numeric intervals.
That is,

Types of Range
In Swift, there are three types of the range:
- Closed Range
- Half-Open Range
- One-Sided Range
Closed Range
A closed range in the Swift includes all the elements in the range from the lower bound to the upper bound.
It is declared using the three-dot(“...”) operator.
Implementation
var range = 1...10
for i in range{
print(i)
}
Output

Half-Open Range
A Half-Open Range in the Swift is somehow similar to the Closed one, except that it does not include the upper bound.
It is declared using the “..<” operator.
Implementation
var range = 1..<10
for i in range{
print(i)
}
Output

One-sided Range
A One-sided range in Swift contains all the elements on one side up to infinite.
We can create the One-sided range using either the “...” or “..<” range.
Implementation1(Using the “...” Operator)
It contains all the elements from lower bound up to the infinity.
var range = 1...
for i in range{
print(i)
}
Output

It continues till infinity…
Must Read Lower Bound in C++
Implementation2(Using “..<” operator)
It contains all the elements from negative infinity to the range.
We can’t print out the result as it starts from the negative infinity. Instead, check whether an element is inside the range or not by using the .contains method as shown below.
var range = ..<5
// check whether -668 is in the range
print(range.contains(-668))
// check whether 5 is in the range
print(range.contains(5))
// check whether 0 is in the range
print(range.contains(0))
Output

Accessing Array Element using Swift Ranges
We can access the array elements using the Swift Ranges, as shown below.
Implementation
var arr = ["This", "is" , "Coding", "Ninjas"]
print(arr[0...1])
print(arr[0...2])
print(arr[0...3])
Output

Few More Points
-
The value of the Lower Bound must Be smaller than the Upper Bound.

-
The value of the Upper and Lower bound must not be negative.
Frequently Asked Questions
What is Swift in iOS Development?
Swift is a robust, intuitive and powerful open language created by Apple that is used to build powerful apps for IOS, MAC, Apple Watch and Apple TV.
Is iOS written in Swift?
Most modern iOS apps are written and developed using Swift.
What languages does Swift use?
Swift has taken references from programming languages including Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and a few more that can be added to the list.
What are the best uses of the Swift?
The best uses of the Swift include building apps for iOS, Mac, Apple TV and Apple Watch.
Conclusion
In this article, we have extensively discussed the Ranges in Swift.
We started with the basic introduction, then we discussed,
- What Swift is
- What is the Range in Swift
-
Types of Range
- Closed Range
- Half-Open Range
- One-sided Range
- After that, we saw how to access the elements of the array using the range
-
Lastly, we discussed a few more points
We hope that this blog has helped you enhance your knowledge regarding Ranges in Swift and if you would like to learn more, check out our articles on The Definitive Swift Tutorial. Do upvote our blog to help other ninjas grow.
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations and much more.!
Happy Reading!