Table of contents
1.
Introduction
2.
About Swift
3.
Swift Ranges
4.
Types of Range
4.1.
Closed Range
4.1.1.
Implementation
4.1.2.
Output
4.2.
Half-Open Range
4.2.1.
Implementation
4.2.2.
Output
4.3.
One-sided Range
4.3.1.
Implementation1(Using the “...” Operator)
4.3.2.
Output
4.3.3.
Implementation2(Using “..<” operator)
4.3.4.
Output
5.
Accessing Array Element using Swift Ranges
5.1.
Implementation
5.2.
Output
6.
Few More Points
7.
Frequently Asked Questions
7.1.
What is Swift in iOS Development?
7.2.
Is iOS written in Swift?
7.3.
What languages does Swift use?
7.4.
What are the best uses of the Swift?
8.
Conclusion
Last Updated: Mar 27, 2024

Ranges in Swift

Author Aniket Majhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome readers! We hope that you are doing well. 

A recent report states that the iOS industry is diminishing due to Android's increasing popularity and robustness. A recent report indicates that iOS is being used by 26.28% of mobile users worldwide, up from 22.71% in 2019, making it the second most popular mobile platform

The iOS industry has gained momentum after the ingress of the Swift. Many businesses are moving their tech stack to Swift, which increased the demand for Swift Developers.

Today, we will be discussing the Ranges in Swift and Its different types. If you want to become one of Swift Developers and want to make an impact in this field, follow this article till the end.

If you want to learn more about Swift, you can refer to The Definitive Swift Tutorial article.

So, without further ado, let's start the topic.

                                 Source

About Swift

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. Swift is easy to use and open source. You can easily implement your idea in Swift and create something incredible.

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

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

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

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

  1. The value of the Lower Bound must Be smaller than the Upper Bound

     
  2. 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 TutorialDo 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!

Live masterclass