相关文章推荐
才高八斗的饺子  ·  Wonder Pets - ...·  3 月前    · 
大鼻子的鸡蛋  ·  电影《地久天长》推广曲MV催泪上线王源讲述跨 ...·  4 月前    · 
内向的花卷  ·  清肠道排宿便对身体有好处吗_头条·  2 年前    · 
细心的佛珠  ·  罕见的“鲸落”究竟是什么?为何被称为世上最“ ...·  2 年前    · 
另类的滑板  ·  村上春树的《1Q84》是否值得阅读?为什么? ...·  2 年前    · 
小百科  ›  【一曰Acg】Gundam MS Bible 高达MS圣经第132期AMX-104 R·贾 ...
机动战士高达 暮光的阿克西斯(动画)
俊秀的牙膏
1 年前
circle-x icon close
automated mobile testing with waldo

Get true E2E testing in minutes, not months.

Waldo provides the best-in-class runtime for all your mobile testing needs.
Learn more
Waldo sessions now support scripting! – Learn more
waldo logo
Sessions
Pricing
Blog
Customers
Log in
Log in
Get started free
Book a demo
App Development

Exploring Ranges in Swift: Useful Tips and Tricks

Nabendu Biswas
Nabendu Biswas
Exploring Ranges in Swift: Useful Tips and Tricks
April 27, 2022
6
min read

Ranges are special features in Swift that aren't available in other languages. They're very powerful and used to select part of an array or string.

In this post, we'll look into the three types of ranges in Swift: closed range, half-open range, and one-sided range. After that, we'll learn about NSRange. Let's start.

Playground Setup

We'll run our Swift code in Playground, which is available in Xcode, the Mac OS code editor. Playground is a feature that allows us to check the result of code without creating an app.

First, we need to open Xcode and click on File > Playground.

Now, on the next screen, select iOS and Blank . After that, press the Next button.

Let's give our Playground the name SwiftRange. After that, click on the Create button.

Now, we'll write the code in Playground.

Swift Range Basics

Range is a special operator in Swift represented by three dots (...) or two dots and a less-than sign (..<). They're helpful to select part of an array or string. There are three types of ranges in Swift:

  • closed range,
  • half-open range, and
  • one-sided range.

Let's take a look at each.

Closed Range

The closed range is given by three dots  (...). In it, we give two numbers, one at the start and the other at the end. This will give us all the numbers in a range including those numbers. So, 1...6 will give us the numbers 1, 2, 3, 4, 5, and 6.

In the example below, we're using it in a "for" loop to print the numbers from 1 to 6.

//Closed Range
for num in 1...6 {
   print("Number is - \(num)")

When we execute this in Playground, we'll get the numbers 1 to 6.

We can also use the closed range to iterate over an array. In the example below, we have an array name lang that contains the name of four mobile app development languages as strings.

Now, we're getting the endIndex of the array by the lang.count method in Swift. It displays a 4, but the last element index is 3, so we subtract one from it.

Now, in the "for" loop, we're using 0...endIndex, which converts to 0...3. Here, inside the "for" loop, we're using the index to get each element and print it. So, the first time it'll print lang[0], which is Swift, and so on.

let lang = ["Swift", "Kotlin", "Flutter", "React Native"]
let endIndex = lang.count-1
for i in 0...endIndex {
    print("Language is - \(lang[i])")

When we execute this in Playground, we'll get the four languages.

We can also get part of the array by passing it in the range. In the example below, we're passing 0...2 in the array.

let lang = ["Swift", "Kotlin", "Flutter", "React Native"]
print(lang[0...2])

Upon execution, this is printing the first three languages from the array.

Half-Open Range

The half-open range is given by two dots and a less-than sign (..<). Similar to the closed range, we give two numbers, one at the start and the other at the end. This will give us all the numbers in the range excluding the second number. So, 1..<6 will give us 1, 2, 3, 4, and 5.

In the example below, we're using it in a "for" loop to print the numbers 1 to 5.

//Half-Open Range
for num in 1..<6 {
    print("Number is - \(num)")

When we execute this in Playground, we'll get the numbers from 1 to 5.

We can also use the half-open range to iterate over an array. In the code below, we've updated our closed-range example.

In it, we don't have to subtract one from the endIndex since the half-open range uses all numbers before the second number.

let lang = ["Swift", "Kotlin", "Flutter", "React Native"]
let endIndex = lang.count
for i in 0..<endIndex {
    print("Language is - \(lang[i])")

When we execute this in Playground, we'll get the four languages.

We can again get part of the array by passing it in the range. In the example below, we're passing 1..<3 in the array.

let lang = ["Swift", "Kotlin", "Flutter", "React Native"]
print(lang[1..<3])

Upon execution, it's printing the second and the third language from the array.

One-Sided Range

The one-sided range can be given by both three dots (...) and two dots and a less-than sign (..<). It has only one number either at the start or end, and it behaves differently in both cases.

If we give the number at the end, it'll take numbers from zero until the number. So, ..<3 will give us 0, 1, and 2. Similarly, ...3, will give us 0, 1, 2, and 3.

We can't use the one-sided range in a "for" loop or it'll give us an error. So, we'll get part of the array by passing it in the range. In the example below, we're passing ..<3 in the array.

let lang = ["Swift", "Kotlin", "Flutter", "React Native"]
print(lang[..<3])

Upon execution, it's printing the first, second, and third language from the array.

If we give the number at the start, it'll take the number from that index until the end.

Again, we can't use it in a "for" loop or it'll give us an error. So, we'll get part of the array by passing it in the range. In the example below, we're passing 2... in the array.

let lang = ["Swift", "Kotlin", "Flutter", "React Native"]
print(lang[2...])

Upon execution, it's printing the third and fourth language from the array.

If we try to use it in a "for" loop to get elements of an array, we'll get an "index out of range" error. We get this error after all languages are printed because Swift won't know when to end.

Using NSRange

In Swift, we don't have many string functions to get the substring. To get the substring, one method is to get it through NSRange. Now, NSRange is equivalent to range in Objective-C.

We still can use NSRange in Swift because it succeeds Objective-C, and most Objective-C features can be used here.

In the example below, we have a constant text, which is an NSString. After that, in the print statement, we're using the substring function. Here, we need to use NSRange and give the starting index and length to get the substring.

let text: NSString = "Swift"
print(text.substring(with: NSRange(location: 0, length: 3)))
 
推荐文章
才高八斗的饺子  ·  Wonder Pets - 萌娘百科萬物皆可萌的百科全書
3 月前
大鼻子的鸡蛋  ·  电影《地久天长》推广曲MV催泪上线王源讲述跨越三十年的亲情 ...
4 月前
内向的花卷  ·  清肠道排宿便对身体有好处吗_头条
2 年前
细心的佛珠  ·  罕见的“鲸落”究竟是什么?为何被称为世上最“浪漫的死亡”?
2 年前
另类的滑板  ·  村上春树的《1Q84》是否值得阅读?为什么? - 知乎
2 年前
Link管理   ·   Sov5搜索   ·   小百科
小百科 - 百科知识指南