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

后端 未结 6 561
予麋鹿
予麋鹿 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:30

    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)
    
    0 讨论(0)
  • 2020-12-04 21:31

    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)
    
    0 讨论(0)
  • 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<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"

    0 讨论(0)
  • 2020-12-04 21:41

    You should simply write

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

    or if you want to go even simpler

    var continousDigitsRange = 0..<0
    
    0 讨论(0)
  • 2020-12-04 21:42

    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
    
    0 讨论(0)
  • 2020-12-04 21:43

    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 <

    0 讨论(0)
提交回复
热议问题