不能调用初始化为类型与类型的参数列表“范围”“(开始:String.Index,端:String.Index)”
问题描述:
let greenHex = hex.substring(with: Range<String.Index>(start: hex.index(hex.startIndex, offsetBy: 2), end: hex.index(hex.startIndex, offsetBy: 4)))
这是Swift3.0,六角是一个字符串,但是这个代码抛出一个错误,说:不能调用初始化为类型与类型的参数列表“范围<String.Index>”“(开始:String.Index,端:String.Index)”
不能调用类型“范围”初始化与类型的 参数列表“(启动:String.Index,结束: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)
非常感谢你,我想我得到了答案。我写的代码就像你说的,它的工作原理。 – Chacha