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.

Nakilon
  • 34,866
  • 14
  • 107
  • 142
mahega
  • 3,231
  • 4
  • 20
  • 32

6 Answers6

88

You should simply write

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

or if you want to go even simpler

var continousDigitsRange = 0..<0
Scott Thompson
  • 22,629
  • 4
  • 32
  • 34
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)
bmichotte
  • 590
  • 4
  • 15
  • 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)' – Hamid Apr 25 '16 at 17:00
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
solenoid
  • 954
  • 1
  • 9
  • 20
  • 1
    Ahh, yeah good point, switched it up - wanted to illustrate the range options in swift. – solenoid Mar 22 '16 at 15:27
  • 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! – user462990 Apr 05 '16 at 11:31
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 <

user462990
  • 5,472
  • 3
  • 33
  • 35
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)
user3608500
  • 835
  • 4
  • 10
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"

Krivvenz
  • 3,911
  • 3
  • 22
  • 32
  • 1
    Doesn't compile with Swift 3. There is no closing `)` after `subStringSize: Int ` – Vlad Sep 30 '16 at 02:55
  • 1
    At the `endIndex` property you missed `fullString.` before `startIndex`, so the correct would be: `let endIndex = fullString.characters.index(fullString.startIndex, offsetBy: subStringSize)` – Mette Oct 01 '16 at 09:38
  • 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! – Krivvenz Oct 01 '16 at 22:45