How to Create Range in Swift

How to create range in Swift?

Updated for Swift 4

Swift ranges are more complex than NSRange, and they didn't get any easier in Swift 3. If you want to try to understand the reasoning behind some of this complexity, read this and this. I'll just show you how to create them and when you might use them.

Closed Ranges: a...b

This range operator creates a Swift range which includes both element a and element b, even if b is the maximum possible value for a type (like Int.max). There are two different types of closed ranges: ClosedRange and CountableClosedRange.

1. ClosedRange

The elements of all ranges in Swift are comparable (ie, they conform to the Comparable protocol). That allows you to access the elements in the range from a collection. Here is an example:

let myRange: ClosedRange = 1...3

let myArray = ["a", "b", "c", "d", "e"]
myArray[myRange] // ["b", "c", "d"]

However, a ClosedRange is not countable (ie, it does not conform to the Sequence protocol). That means you can't iterate over the elements with a for loop. For that you need the CountableClosedRange.

2. CountableClosedRange

This is similar to the last one except now the range can also be iterated over.

let myRange: CountableClosedRange = 1...3

let myArray = ["a", "b", "c", "d", "e"]
myArray[myRange] // ["b", "c", "d"]

for index in myRange {
print(myArray[index])
}

Half-Open Ranges: a..<b

This range operator includes element a but not element b. Like above, there are two different types of half-open ranges: Range and CountableRange.

1. Range

As with ClosedRange, you can access the elements of a collection with a Range. Example:

let myRange: Range = 1..<3

let myArray = ["a", "b", "c", "d", "e"]
myArray[myRange] // ["b", "c"]

Again, though, you cannot iterate over a Range because it is only comparable, not stridable.

2. CountableRange

A CountableRange allows iteration.

let myRange: CountableRange = 1..<3

let myArray = ["a", "b", "c", "d", "e"]
myArray[myRange] // ["b", "c"]

for index in myRange {
print(myArray[index])
}

NSRange

You can (must) still use NSRange at times in Swift (when making attributed strings, for example), so it is helpful to know how to make one.

let myNSRange = NSRange(location: 3, length: 2)

Note that this is location and length, not start index and end index. The example here is similar in meaning to the Swift range 3..<5. However, since the types are different, they are not interchangeable.

Ranges with Strings

The ... and ..< range operators are a shorthand way of creating ranges. For example:

let myRange = 1..<3

The long hand way to create the same range would be

let myRange = CountableRange<Int>(uncheckedBounds: (lower: 1, upper: 3)) // 1..<3

You can see that the index type here is Int. That doesn't work for String, though, because Strings are made of Characters and not all characters are the same size. (Read this for more info.) An emoji like , for example, takes more space than the letter "b".

Problem with NSRange

Try experimenting with NSRange and an NSString with emoji and you'll see what I mean. Headache.

let myNSRange = NSRange(location: 1, length: 3)

let myNSString: NSString = "abcde"
myNSString.substring(with: myNSRange) // "bcd"

let myNSString2: NSString = "acde"
myNSString2.substring(with: myNSRange) // "c" Where is the "d"!?

The smiley face takes two UTF-16 code units to store, so it gives the unexpected result of not including the "d".

Swift Solution

Because of this, with Swift Strings you use Range<String.Index>, not Range<Int>. The String Index is calculated based on a particular string so that it knows if there are any emoji or extended grapheme clusters.

Example

var myString = "abcde"
let start = myString.index(myString.startIndex, offsetBy: 1)
let end = myString.index(myString.startIndex, offsetBy: 4)
let myRange = start..<end
myString[myRange] // "bcd"

myString = "acde"
let start2 = myString.index(myString.startIndex, offsetBy: 1)
let end2 = myString.index(myString.startIndex, offsetBy: 4)
let myRange2 = start2..<end2
myString[myRange2] // "cd"

One-sided Ranges: a... and ...b and ..<b

In Swift 4 things were simplified a bit. Whenever the starting or ending point of a range can be inferred, you can leave it off.

Int

You can use one-sided integer ranges to iterate over collections. Here are some examples from the documentation.

// iterate from index 2 to the end of the array
for name in names[2...] {
print(name)
}

// iterate from the beginning of the array to index 2
for name in names[...2] {
print(name)
}

// iterate from the beginning of the array up to but not including index 2
for name in names[..<2] {
print(name)
}

// the range from negative infinity to 5. You can't iterate forward
// over this because the starting point in unknown.
let range = ...5
range.contains(7) // false
range.contains(4) // true
range.contains(-1) // true

// You can iterate over this but it will be an infinate loop
// so you have to break out at some point.
let range = 5...

String

This also works with String ranges. If you are making a range with str.startIndex or str.endIndex at one end, you can leave it off. The compiler will infer it.

Given

var str = "Hello, playground"
let index = str.index(str.startIndex, offsetBy: 5)

let myRange = ..<index // Hello

You can go from the index to str.endIndex by using ...

var str = "Hello, playground"
let index = str.index(str.endIndex, offsetBy: -10)
let myRange = index... // playground

See also:

  • How does String.Index work in Swift
  • How does String substring work in Swift

Notes

  • You can't use a range you created with one string on a different string.
  • As you can see, String ranges are a pain in Swift, but they do make it possibly to deal better with emoji and other Unicode scalars.

Further Study

  • String Range examples
  • Range Operator documentation
  • Strings and Characters documentation

Swift: create a range around a given number

Regarding your question one you can extend Range/ClosedRange and create a custom initialiser to behave as you want:

extension Range where Bound: AdditiveArithmetic & ExpressibleByIntegerLiteral {
init(center: Bound, tolerance: Bound) {
self.init(uncheckedBounds: (lower: center - tolerance, upper: center + tolerance + 1))
}
}

extension ClosedRange where Bound: AdditiveArithmetic {
init(center: Bound, tolerance: Bound) {
self.init(uncheckedBounds: (lower: center - tolerance, upper: center + tolerance))
}
}

Regarding your question two about rounding, as already mentioned in comments you can multiply your value by n, round it and then divide by n again:

extension FloatingPoint {
func rounded(to value: Self, roundingRule: FloatingPointRoundingRule = .toNearestOrAwayFromZero) -> Self { (self/value).rounded(roundingRule) * value }
}

16.rounded(to: 15)  // 15

How to create a range for number in swift

Range has a method contains that you can use to check to see if a particular value lies between the range's endpoints. But you're probably better off using the switch statement--with its interval matching, it's basically designed for this scenario.

You can do something like the following:

// assume score is an Int defined above
switch score {
case 0...20:
medal = SKSpriteNode(imageNamed: "medalB")
case 21...49:
medal = SKSpriteNode(imageNamed: "medalS")
case 50...60:
medal = SKSpriteNode(imageNamed: "medalG")
default:
print("It is inconceivable that you are this good.")
}

Keep in mind that a switch statement has to be exhaustive, that is there must be a case to match every possible value of the expression on which you are switching. That's why I added the default above.

Getting a empty RangeString.Index in Swift

Instead of trying to create an empty range, I would suggest creating an empty Substring in case there was no match. Range can be quite error-prone, so using this approach you can save yourself a lot of headaches.

let match: Substring
if let range = data.range(of: "[a-zA-Z]at", options: .regularExpression) {
match = data[range]
} else {
match = ""
}
print(match)

Creating RangeString.Index from constant Ints

You need to reference the startIndex of a specific string, then advance:

let longString = "Supercalifragilistic"
let startIndex = longString.startIndex
let range = Range(start: startIndex, end: startIndex.advancedBy(3))

How to create a range of CGPoints (SWIFT)

Sounds like what your really need is a GCRect.

let rect = CGRect(x: 155, y: 132, width: 5, height: 4)

Then, to know if your touched point is inside this rect, just do:

rect.contains(touchedPoint)

How to create an array with incremented values in Swift?

Use the ... notation / operator:

let arr1 = 0...4

That gets you a Range, which you can easily turn into a "regular" Array:

let arr2 = Array(0...4)

How to set a custom random range in SwiftUI/Swift

You can't use Int.random(in:) like that, because self isn't available. Instead, you should create the initial random number from the init where you have the range passed in - or you could create a default range in the initializer by changing it to this:

init(range: ClosedRange<Int> = 0 ... 100) {
/* ... */
}

Here is an example of how you could do this, where the random number only is updated when you choose to (in this case, pressing a button).

struct ContentView: View {
var body: some View {
RandomNumberView(range: 0 ... 100)
}
}
struct RandomNumberView: View {
@State var range: ClosedRange<Int>
@State var randomNumber: Int

init(range: ClosedRange<Int>) {
self.range = range
randomNumber = .random(in: range)
}

var body: some View {
List {
Text("Random number: \(randomNumber)")

Stepper(
"Minimum: \(range.lowerBound)",
value: Binding<Int>(
get: { range.lowerBound },
set: { range = $0 ... range.upperBound }
),
in: 0 ... range.upperBound
)

Stepper(
"Maximum: \(range.upperBound)",
value: Binding<Int>(
get: { range.upperBound },
set: { range = range.lowerBound ... $0 }
),
in: range.lowerBound ... 100
)

Button("Randomize") {
randomNumber = .random(in: range)
}
}
}
}

Result:

Result



Related Topics



Leave a reply



Submit