我刚刚转换了我的小应用程序,但我发现了这个错误: 'substring(from :)'已被弃用:请使用字符串切片下标和'partial range from'运算符
我的代码是:
let dateObj = dateFormatterFrom.date(from: dateStringa)
if dateObj != nil {
cell.detailTextLabel?.text = dateFormatterTo.string(from:(dateObj!))
} else {
let index = thisRecord.pubDate.index(thisRecord.pubDate.startIndex, offsetBy: 5)
cell.detailTextLabel?.text = thisRecord.pubDate.substring(from: index)
}
答案 0 :(得分:24)
按照以下示例修复此警告
let testStr = “Test Teja”
let finalStr = testStr.substring(to: index) // Swift 3
let finalStr = String(testStr[..<index]) // Swift 4
let finalStr = testStr.substring(from: index) // Swift 3
let finalStr = String(testStr[index...]) // Swift 4
//Swift 3
let finalStr = testStr.substring(from: index(startIndex, offsetBy: 3))
//Swift 4
let reqIndex = testStr.index(testStr.startIndex, offsetBy: 3)
let finalStr = String(testStr[..<reqIndex])
答案 1 :(得分:11)
取代substring
使用suffix
。使用如下:
cell.detailTextLabel?.text = String(thisRecord.pubDate.suffix(from: index))
答案 2 :(得分:10)
这意味着您应该使用新的部分范围运算符作为upperBound:
let str = "Hello World !!!"
if let index = str.range(of: "Hello ")?.upperBound {
let string = String(str[index...]) // "World !!!"
}
在你的情况下
cell.detailTextLabel?.text = String(thisRecord.pubDate[index...]))
答案 3 :(得分:3)
我的大部分字符串都有A-Za-z和0-9内容。不需要困难 索引处理。 String的这个扩展基于熟悉的LEFT / MID和RIGHT函数。
extension String {
// LEFT
// Returns the specified number of chars from the left of the string
// let str = "Hello"
// print(str.left(3)) // Hel
func left(_ to: Int) -> String {
return "\(self[..<self.index(startIndex, offsetBy: to)])"
}
// RIGHT
// Returns the specified number of chars from the right of the string
// let str = "Hello"
// print(str.left(3)) // llo
func right(_ from: Int) -> String {
return "\(self[self.index(startIndex, offsetBy: self.length-from)...])"
}
// MID
// Returns the specified number of chars from the startpoint of the string
// let str = "Hello"
// print(str.left(2,amount: 2)) // ll
func mid(_ from: Int, amount: Int) -> String {
let x = "\(self[self.index(startIndex, offsetBy: from)...])"
return x.left(amount)
}
}
答案 4 :(得分:0)
如果您希望获得具有特定偏移量且没有上限的子字符串,请执行以下操作:
let index = thisRecord.pubDate.index(thisRecord.pubDate.startIndex, offsetBy: 5)
cell.detailTextLabel?.text = String(thisRecord.pubDate[index...]
通过这种方式,您可以从现有的String
创建一个新的String thisRecord.pubDate
对象,并从指定的索引到原始String
的结束索引中的任何内容。
答案 5 :(得分:0)
* hard nofile 990000
* soft nofile 990000
root hard nofile 990000
root soft nofile 990000
上面的代码是“部分范围自” 看看这个How can I use String slicing subscripts in Swift 4?
答案 6 :(得分:0)
在Swift 5中,它是:
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])
}
}