在 Swift 3.0 中循环遍历子字符串的最佳方法是什么?
var start = s.startIndex
var end = s.index(s.endIndex, offsetBy: -10)
for i in start...end {
}
以下代码会引发错误:Type ClosedRange<String.Index> (aka ‘ClosedRange<String.CharacterView.Index>’) does not conform to Sequence protocol
.
最佳答案
start
和end
是String.Index
的类型,不能在for循环中使用;相反,您可以获得以下范围内的 substring
:
var start = s.startIndex
var end = s.index(s.endIndex, offsetBy: -10)
let range = Range(uncheckedBounds: (lower: start, upper: end))
let subString = s.substring(with: range)
关于swift - 在 Swift 3.0 中循环遍历子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42662298/