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

后端 未结 6 583
予麋鹿
予麋鹿 2020-12-04 20:54

I\'m using the following code:

var continousDigitsRange:Range = Range(start: 0, end: 0)

Since update to Xcode 7.3 (Sw

6条回答
  •  一向
    一向 (楼主)
    2020-12-04 21:39

    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 {
        let startIndex = fullString.characters.index(fullString.startIndex, offsetBy: fromIndex)
        let endIndex = fullString.characters.index(startIndex, offsetBy: subStringSize)
    
        let subStringRange = startIndex..

    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"

提交回复
热议问题