Swift 4 'substring(from:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator

前端 未结 7 2050
情话喂你
情话喂你 2020-12-30 23:59

i\'ve just converted my little app but i\'ve found this error: \'substring(from:)\' is deprecated: Please use String slicing subscript with a \'partial range from\' operator

相关标签:
7条回答
  • 2020-12-31 00:40

    In Swift 5, it is:

    extension String {
    
        func index(from: Int) -> Index {
            return self.index(startIndex, offsetBy: from)
        }
    
        func substring(from: Int) -> String {
            let fromIndex = index(from: from)
            return String(self[fromIndex...])
        }
    
        func substring(to: Int) -> String {
            let toIndex = index(from: to)
            return String(self[..<toIndex])
        }
    
        func substring(with r: Range<Int>) -> String {
            let startIndex = index(from: r.lowerBound)
            let endIndex = index(from: r.upperBound)
            return String(self[startIndex..<endIndex])
        }
    }
    
    0 讨论(0)
提交回复
热议问题