'init(start:end:)' is deprecated: it will

2019-01-13 09:27发布

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.

6条回答
贪生不怕死
2楼-- · 2019-01-13 09:41

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
查看更多
手持菜刀,她持情操
3楼-- · 2019-01-13 09:53

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)
查看更多
【Aperson】
4楼-- · 2019-01-13 09:54

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)
查看更多
对你真心纯属浪费
5楼-- · 2019-01-13 09:55

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"

查看更多
祖国的老花朵
6楼-- · 2019-01-13 09:56

You should simply write

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

or if you want to go even simpler

var continousDigitsRange = 0..<0
查看更多
贼婆χ
7楼-- · 2019-01-13 09:56

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 <

查看更多
登录 后发表回答