我在操场上尝试过这个:
class SomeClass {
private let input: String.CharacterView
private var position: String.CharacterView.Index
...
private func advance() {
position += 1
} // advance
} // SomeClass
Run Code Online (Sandbox Code Playgroud)
...但是我收到以下错误消息:
错误:二进制运算符'+ ='无法应用于类型为'String.CharacterView.Index'和'Int'的操作数
我尝试增加此索引的原因是因为我想执行以下操作:
input[position]
Run Code Online (Sandbox Code Playgroud)
...并在input中访问特定字符。
至此我已经知道我不能在位置上使用这个二进制运算符。什么是可行的解决方案?
我正在使用Swift 3和Xcode 8.0 beta 6(8S201h)
在Swift 3中,“集合移动其索引”,比较了 关于Swift进化的集合和索引的新模型。
就你而言
private func advance() {
input.formIndex(&position, offsetBy: 1)
}
Run Code Online (Sandbox Code Playgroud)
会使索引位置增加一。在这里,您必须确保索引不等于endIndex,或者使用
private func advance() {
input.formIndex(&position, offsetBy: 1, limitedBy: input.endIndex)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1191 次 |
| 最近记录: |