Range Examples
This page was last reviewed on Aug 22, 2023.
Dot Net Perls
Range. In Swift 5.8 we can create a range with special syntax. For example, the numbers 0, 1 and 2 are a range. It is possible to loop over a range.
Bounds. With startIndex and endIndex, we can test the bounds of a range. Count returns the number of elements. And index() with offsetBy changes the start or end.
Loop example. Here we create a constant range of the values 0, 1 and 2. With this syntax, the 0 and the 2 are both included. We use a for-in loop over the range.
Here The term "range" is just an identifier for the range of values. This syntax includes 0 and 2.
Detail A range can be specified directly in the loop expression (after in). In this usually a clearer way to write a loop.
// Create a range. let range = 0...2 // Use a for-in loop over the range. for value in range { print(value) }
0 1 2
StartIndex, endIndex. This example shows the startIndex and endIndex properties. These return the first and last indexes in the range.
Note There is some complexity here. EndIndex here is one greater than the last included value.
Note 2 With count, we get an Int that is equal to the number of values within the range. Here that is 6.
let range = 5...10 // A range has a startIndex and an endIndex. // ... The endIndex is one more than the last included index. print(range.startIndex) print(range.endIndex) // Count returns the number of elements in the range. print(range.count)
inRange(5) pastEnd 6
Half-open ranges. In Swift we can create ranges that do not include the second (end) number. These use a "less than" sign. These are called half-open ranges.
Tip With this operator, we can get a range of all the indexes in an array without subtracting one from the array's count.
// This range does not include the last number. let values = 0..<5 // Loop over values in the exclusive-end range. for value in values { print(value) }
0 1 2 3 4
Negative numbers. Usually ranges have positive starts and ends. But we can have negative numbers in our ranges. The start (even if negative) must be less than the end.
Note The range here is specified directly within the for-in loop. This makes for simpler code.
// A range can start at a negative number. for id in -4...2 { print(id) }
-4 -3 -2 -1 0 1 2
Index, offsetBy. Sometimes we want to create a range based on an existing one. We can use index (with offsetBy) on the startIndex or endIndex. Here we increment the range's start by 2.
Tip With index() we can reduce an index by passing a negative Int as the second argument.
Note When we print a range, we see information about the range, not just a start and end integer pair.
var range = 0...3 // Create a new range based on an existing range. // ... Advance the startIndex by 2. var x = range.index(range.startIndex, offsetBy: 2)..<range.endIndex // Display ranges. print(range) print(x)
0...3 inRange(2)..<pastEnd
Invalid range. A range's start must be less than or equal to its end. It cannot be larger. Here we encounter the "can't form Range" error in a compiled Swift program.
// This will cause an error. var range = 100...0 // Not reached. print(range)
fatal error: Can't form Range with end < start (lldb)
Range of array indexes. A half-open range can return all valid indexes for an array. The end is one less than the count. Here we use for-in on all array indexes.
// This string array has three elements. let animals: [String] = ["bird", "cat", "fox"] // Loop through all indexes in the array. for index in 0..<animals.count { print(index) print(animals[index]) }
0 bird 1 cat 2 fox
Substring note. Ranges are useful when taking substrings. The index() offsetBy method helps us index into a string. And then we can access the string by a range.
A history lesson. Range syntax has been changed in Swift 2 and Swift 3. This is confusing. But in many ways the newer syntax forms are clearer and easier to use.
Ranges are used with arrays and strings. They can create a sequence for a for-in loop. Closed ranges, and half-open ones, are common in Swift programs.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Aug 22, 2023 (edit).
© 2007-2024 Sam Allen.