自Xcode 10起,我在以下代码的返回行中收到一条错误消息
Cannot invoke initializer for type 'Range<_>' with an argument list of type '(Range<String.Index>)'
,该错误消息是用于在需要时获取子字符串的扩展名。
我在Cannot invoke initializer for type 'Range<String.Index>' with an argument list of type '(Range<String.Index>)'找到了Martin R给出的另一个很好的答案,但是还没有找到解决我的问题的方法。
extension String {
subscript (i: Int) -> Character { return self[index(startIndex, offsetBy: i)] }
subscript (i: Int) -> String { return String(self[i] as Character) }
subscript (r: Range<Int>) -> String {
let start = index(startIndex, offsetBy: r.lowerBound)
let end = index(startIndex, offsetBy: r.upperBound)
return String(self[Range(start ..< end)])
}
}