In Swift 4, I\'m getting this error when I try to take a Substring
of a String
using subscript syntax.
\'subscript\' is unavaila
Based on p-sun's answer
Swift 4
extension StringProtocol {
subscript(bounds: CountableClosedRange) -> SubSequence {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(start, offsetBy: bounds.count)
return self[start..) -> SubSequence {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(start, offsetBy: bounds.count)
return self[start..
Notable changes:
StringProtocol
. This allows adopters such as Substring
to also gain these subscripts.String
twice. The index
method is O(n) where n is the offset from i.