What is Swift ranges and how it works?

Swift is an object-oriented programming language used to create apps for iOS, macOS, watchOS, tvOS, and z/OS. It is a well-known and amazing programming language all around the world. Swift ranges must be mentioned if you wish to operate successfully with the Swift programming language. If you are unfamiliar with the term range quick. In this article, we’ll introduce you to range swift and demonstrate you how to use it in iOS programming. 

What are Swift ranges?

 

To work effectively with Swift ranges, you must first thoroughly understand their description. Swift ranges are sophisticated programming language features. Swift ranges are used to define numbers in Swift that fall within the lower and upper boundaries. They may be used to build array slices, check if a value is in a range, and so on.

Swift ranges

Swift ranges are a set of values that span two numeric intervals. Then look at the syntax below:

var numbers = 1…4

Swift ranges types

The Swift language divides into three types of Swift ranges. Let’s explore them. 

1. Swift ranges closed

A closed range contains all of the numbers between the lower and upper boundaries.

// 1…4 is close range
for numbers in 1…4 {
  print(numbers)
}

If you follow the instructions above, you will obtain the following result:

1
2
3
4

As you can see, we’ve created a limited range 1…4 in the example above. Because it is a closed range, it covers all integers between 1 and 4, including the bottom bound (1) and upper bound (2). (4). Furthermore, we utilized the Swift for loop to retrieve all of the numbers in the range.

2. Half-Open Range – Range swift

The second form of Swift range is the Half-Open range. The semi-open range, in reality, encompasses all values between the lower and higher boundaries. Otherwise, it excludes the upper bound. In addition, the.. operator is used to declare it. So, have a look at the sample below to see how this works:

for numbers in 1..<4 {
  print(numbers)
}

The outcome is as follows:

1
2
3

3. One-sided Range

We can make one-sided scopes by using the … or the ..< operators.

A one-sided range contains elements that extend in one direction to infinity. Eg,

let range1 = ..<2

Here …<2 is a one-sided range. It contains all elements ranging from 2 to -∞. Likewise, the range

let range2 = 2...

contains all elements from 2 to +∞.

We can confirm this by looking for a specific number in the range. As an example,

// one-sided range using 
// ..< operator
let range1 = ..<2

// check if -9 is in the range
print(range1.contains(-1))

// one-sided range using
// ... operator
let range2 = 2...

// check if 33 is in the range
print(range2.contains(33))

The outcome is as follows:

true
true

Here, we used the contain( ) method to see if a specific number was in the range.

How you can work with it in iOS development

How to work with Ranges and Arrays?

For starters, why not start putting those ranges to use? Fast ranges and arrays are two of the most popular examples of the first scenario. In real-world application development, scopes are frequently used in combination with arrays.

Let’s refer to the following codes:

let names = [“Bernard”, “Dolores”, “Maeve”, “Lee”]
for i in 0..<names.count {
    print(names[i])
}

As you can see, in the above example, we cycle through the array of string names using a for loop. Rather than iterating over the array directly, we’ll establish a scope and utilize it to obtain specific array items by key.

The range on which we are working is 0..<names.count. Except for 0, 1, 2, and 3, signifies 0 to 4. The integers in the range are used to represent the array indices.

In addition, we utilize subscript syntax in the for loop to access particular array items using the array index – integer from the range. In addition, if I equal 2 at any time throughout the loop, for example, chúng tôi sẽ in ra “Maeve”.

Of course, we don’t utilize the simpler name-in-name syntax in this situation. Using indices to iterate across collections and ranges is a frequent approach in iOS development. Consider binary search or insertion sort, which both operate with array indices and the ranges that include them.

Array Slicing with Range swift

Actually, scopes are helpful for more than just the things listed above. Range operators can be used to extract slices from an array. Let’s look at this to find out:

let names = [“Ford”, “Arthur”, “Zaphod”, “Marvin”, “Eddie”]
let slice = names[0…2]
print(slice)

Let’s look at the following lines of code:

In the preceding code, we defined an array of named strings. With the closed range operator, we’re “selecting” a subset of that array, particularly items 0, 1, and 2.

[“Ford”, “Arthur”, “Zaphod”] is printed out as a consequence. 

Furthermore, the outstanding thing is that the slice constant’s type is ArraySlice<String>. In particular, it isn’t an array as well as not a duplicate of the names array. A slice, on the other hand, is a transient “view” into the source names array.

Additionally, a slice can be thought of as an index-only replica of the array that refers to the old names array. If necessary, Swift will convert the slice to an actual Array type, or you can use the Array(···) initializer manually.

CONCLUSION

Swift is a very popular and amazing object-oriented programming language. If you are interested in Swift then definitely know about swift ranges. In this article, we have everything you need to understand swift ranges. We hope it will be of help to you.

ONEXT DIGITAL employs a team of professionals and skilled developers who collaborate closely with designers to create user-friendly eCommerce websites, CMS websites, and web applications. If you’re having problems with this experience or want to learn more, contact us and we’ll help you figure it out.

>> Read also: 

Easy Ways To Round Doubles In Java To Two Decimal Places