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

陌路散爱 提交于 2019-12-28 05:06:18

问题


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.


回答1:


You should simply write

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

or if you want to go even simpler

var continousDigitsRange = 0..<0



回答2:


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)



回答3:


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



回答4:


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 <




回答5:


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)



回答6:


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"



来源:https://stackoverflow.com/questions/36157777/initstartend-is-deprecated-it-will-be-removed-in-swift-3-use-the-o

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!