无法使用类型为“(start: String.Index, end: String.Index)”的参数列表调用类型“Range”的初始值设定项

2024-04-07

let greenHex = hex.substring(with: Range<String.Index>(start: hex.index(hex.startIndex, offsetBy: 2), end: hex.index(hex.startIndex, offsetBy: 4)))

这是 Swift3.0,十六进制是一个字符串,但是这段代码会抛出一个错误:

无法使用类型“Range”调用初始值设定项 类型为“(start: String.Index, end: String.Index)”的参数列表


Range.init(start:end:)构造函数在 Swift 3.0 中被删除,因此您可以像下面这样初始化一个范围:

let range = hex.index(hex.startIndex, offsetBy: 2)..<hex.index(hex.startIndex, offsetBy: 4)

它返回一个半开类型范围<String.Index>。然后,您可以用它执行以下操作:

hex.substring(with: range)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法使用类型为“(start: String.Index, end: String.Index)”的参数列表调用类型“Range”的初始值设定项 的相关文章

随机推荐