【发布时间】:2020-02-10 08:50:13
【问题描述】:
我是 Swift 的新手。我使用 Swift 5.1 我想从字符串中获取子字符串,但我做不到。 我已经尝试了几种解决方案(here、here),但这些对我不起作用。
我试过这样:
func substring(x : Int, y : Int, s : String) -> String {
let start = s.index(s.startIndex, offsetBy: x);
let end = s.index(s.startIndex, offsetBy: y);
return s[start..<end];
}
print(substring(x: 0, y: 2, s: "abcde"))
/tmp/306AE87E-57C7-417C-B2EF-313A921E75B9.BuUdsc/main.swift:6:11: 错误:下标 'subscript(_:)' 需要类型 'String.Index' 和 'Int' 等价 return s[start..(bounds: R) -> String where R : RangeExpression, R.Bound == Int { get } ^
非常感谢您的帮助。谢谢。
【问题讨论】:
-
错误的错误信息是bug:bugs.swift.org/browse/SR-11702