'init(start:end:)' is deprecated: it will be removed in Swift 3. Use the '..<' operator
Asked Answered
S

6

72

I'm using the following code:

var continousDigitsRange:Range<Int> = Range<Int>(start: 0, end: 0)

Since update to Xcode 7.3 (Swift 2.2) I got the following hint:

'init(start:end:)' is deprecated: it will be removed in Swift 3. Use the '..<' operator.

For me is not clear how to "translate" it correctly with "using the '..<' operator.

Sabba answered 22/3, 2016 at 14:55 Comment(2)
for item in 0 ..< items { } for item in 0 ... 100 { }Holy
https://mcmap.net/q/23438/-how-to-create-range-in-swiftCigar
N
88

You should simply write

var continousDigitsRange1:Range<Int> = 0..<0

or if you want to go even simpler

var continousDigitsRange = 0..<0
Neoclassic answered 22/3, 2016 at 14:59 Comment(0)
L
35

Also worth noting, to substringWithRange a String, you can now use

let theString = "Hello, how are you"
let range = theString.startIndex.advancedBy(start) ..< theString.startIndex.advancedBy(end)
theString.substringWithRange(range)
Lavadalavage answered 22/3, 2016 at 15:34 Comment(1)
One small thing is that if you are counting from the end of the string, use endIndex instead of start like bellow: 'let range = theString.startIndex.advancedBy(start) ..< theString.endIndex.advancedBy(end)'Rixdollar
A
8

The closed range operator (a...b) defines a range that runs from a to b, and includes the values a and b. The value of a must not be greater than b.

The half-open range operator (a..<b) defines a range that runs from a to b, but does not include b. It is said to be half-open because it contains its first value, but not its final value. As with the closed range operator, the value of a must not be greater than b. If the value of a is equal to b, then the resulting range will be empty.

The Swift Programming Language (Swift 2.2) - Basic Operators

var continousDigitsRange:Range<Int> = Range<Int>(start: 0, end: 0)
--to--
var continousDigitsRange:Range<Int> = 0..<0
Ashaashamed answered 22/3, 2016 at 15:7 Comment(2)
Ahh, yeah good point, switched it up - wanted to illustrate the range options in swift.Ashaashamed
What ends up in continuousDigitsRange? Looks like a nil to me. What use could this range be put to? I'm trying to learn too!Suffering
S
2

to show bmichotte's answer in full...

let theString = "Hello, how are you today my friend"
    let start = 3
    let end = 15
    let range = theString.startIndex.advancedBy(start) ..< theString.startIndex.advancedBy(end)
    let p = theString.substringWithRange(range)
    print("this is the middle bit>\(p)<")

this produces this is the middle bit>lo, how are <

Suffering answered 5/4, 2016 at 11:36 Comment(0)
C
1

Adding some points with reference to swift 3.0

//Countable Range Example.

let range1 = 0..<5

Countable Closed Range Example

let range2 = 0...5

//Range from bounds

let range = Range(uncheckedBounds: (range1.lowerBound,range1.upperBound))

//To get the distance from substringRange.

let str = "Hello, how are you"
let substringRange = str.characters.indices

// Below Swift 3.0

let length = substringRange.distance(from: substringRange.startIndex, to: substringRange.endIndex)

//For Swift 3.0

let length2 = str.distance(from: substringRange.startIndex, to: substringRange.endIndex)
Conquest answered 20/6, 2016 at 13:25 Comment(0)
D
1

I have always had a function to get the substring range of a string. Here is my updated function for Swift 3:

func getSubStringRange(fullString: String, fromIndex: Int, subStringSize: Int) -> Range<String.Index> {
    let startIndex = fullString.characters.index(fullString.startIndex, offsetBy: fromIndex)
    let endIndex = fullString.characters.index(startIndex, offsetBy: subStringSize)

    let subStringRange = startIndex..<endIndex

    return subStringRange
}

The function is pretty self explanatory - You pass in a string(fullString), the index of that string where the substring starts(fromIndex) and how big the subString is(subStringSize).

Example:

let greeting = "Hi, my name is Nathaniel"
let getName = greeting[getSubStringRange(fullString: greeting, fromIndex: 15, subStringSize: 9)]

print("Name: \(getName)")

-> Prints: "Name: Nathaniel"

Distributor answered 22/6, 2016 at 10:44 Comment(3)
Doesn't compile with Swift 3. There is no closing ) after subStringSize: Int Chillon
At the endIndex property you missed fullString. before startIndex, so the correct would be: let endIndex = fullString.characters.index(fullString.startIndex, offsetBy: subStringSize)Bight
Thanks guys. I must have been asleep when I typed that! I had to manually type the whole lot rather than a copy / paste directly from my code as I was on a Windows PC when posting!Distributor

© 2022 - 2024 — McMap. All rights reserved.