swift string.extension.subscript.swift

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift string.extension.subscript.swift相关的知识,希望对你有一定的参考价值。

extension String.CharacterView {
    subscript(sequentialAccess i: Int) -> Character {
        let index = self.index(startIndex, offsetBy: i)
        return self[index]
    }
    subscript(sequentialAccess range: Range<Int>) -> String.CharacterView {
        let start = self.index(startIndex, offsetBy: range.lowerBound)
        let end = self.index(start, offsetBy: range.count)
        return self[start..<end]
    }
    subscript(sequentialAccess range: ClosedRange<Int>) -> String.CharacterView {
        let start = self.index(startIndex, offsetBy: range.lowerBound)
        let end = self.index(start, offsetBy: range.count - 1)
        return self[start...end]
    }
}
let s = "12345"
s.characters[sequentialAccess: 1]
// → 2
String(s.characters[sequentialAccess: 1..<3])
// → 23
String(s.characters[sequentialAccess: 1...3])
// → 234

以上是关于swift string.extension.subscript.swift的主要内容,如果未能解决你的问题,请参考以下文章

Swift入门系列--Swift官方文档(2.2)--中文翻译--About Swift 关于Swift

swift 示例BS swift.swift

swift swift_bug.swift

ios 整理(一)swift和oc的区别

swift swift_extension5.swift

swift swift_optional4.swift