我试图将Swift 3代码转换为Swift 4.但是这个错误显示:
"substring(from :)'已弃用:请使用字符串切片下标和'partial range from'运算符."
我的代码是:
extension String {
func substring(_ from: Int) -> String {
return self.substring(from: self.characters.index(self.startIndex, offsetBy: from))
}
}
Run Code Online (Sandbox Code Playgroud)
你应该有:
extension String {
func substring(_ from: Int) -> String {
let start = index(startIndex, offsetBy: from)
return String(self[start ..< endIndex])
}
}
Run Code Online (Sandbox Code Playgroud)
在swift中,你可以使用切片获得子串 - 这样的结构:string[startIndex ..< endIndex].字符串索引是swift中的一种特殊类型,所以你不能使用简单的整数 - 你必须得到适当的索引来实例调用index(_,offsetBy:)或使用预定义的startIndex和endIndex.
| 归档时间: |
|
| 查看次数: |
5881 次 |
| 最近记录: |