Swift Ranges

Low Key…A Major Key

Collections in the Swift programming language group together elements of the same type. Swift collections (Arrays, Sets, and Dictionaries) store values the same way people of similar interests or personalities hang out together and form a squad.

A Range(…) is the original squad

Apple defines a Range as an “interval over a comparable type” — usually of type Integer (Int) or Double. But, Ranges can be considered a collection also — a collection of indexes. And since Swift collections all have indexes, Arrays, Sets, and Dictionary Arrays are collections of indexes (aka Ranges) on a very base level:

scores = [88, 90, 88, 93, 97] is…

Array = [0, 1, 2, 3, 4] → 0…4

sports = [“soccer”, “basketball”, “rugby”, “football” ] is…

Set = [0, 1, 2, 3] → 0…3

states = [New York: NY, California: CA, Nevada: NV] is…

Dictionary Array = [0:1, 1:2, 2:3] → 0…2

There are two types of Ranges:

What Ranges Can Do:

Ranges can change multiple values in an Array at once:

In the example above, two items or values are being added to the shoppingList array specifically at the index range 3…4. “bananas” and “ice cream” get removed and replaced with “cheese” and “grapes. Here, a Range makes updating multiple values of an Array more efficient, so we do not have to change the values one index at a time by doing shoppingList[index] = “new item” more than once. This also works if the total number of items being removed is not equal to the total number of items being added.

Ranges can define a condition:

Occasionally, we might want our program to run only “if” certain conditions are true or false. For conditions that can be expressed in numerical values, such as power level in a game, temperature in a weather app, or the speed of a vehicle, we can set the necessary parameters using a Range. In the above example, the condition for speed is written as a Boolean statement (>= 0 && <= 90), but we can translate or read it as a Range starting at 0 up to and including 90 (0…90).

We can create a range out of String values:

In the above example, we create a Range out of String values by isolating a set of values (“TOBE”: indexes 0, 1, 2, 3) out of a larger String (“OCTOBER”). Then we define index parameters for that new sub-String with an index range of 1…3. Next a subRange is defined of half open Range type ( ..<), which says when the subRange is called on a String, get the String values of the Range starting at index 1 up to but not including 3 (1…2). This gives the value “OB”.

Ranges have “cool” methods to call:

Regularly called Range methods include .count, .isEmpty, and .index(of:), though there are other useful (and cooler) methods available for Ranges like .overlaps to determine if two ranges intersect each other; .distance to calculate the distance between two index values; .contains to check if a value exists in a range; and .split which separates a range at a given number to create two ranges.

Understanding the functionality of Ranges and recognizing opportunities to use them can aid our problem-solving process and optimize the efficiency of our code.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.