在更新到显然是Swift 4.1.50附带的Xcode 10 beta之后,我看到以下错误,我不确定如何解决:
After updating to Xcode 10 beta, which apparently comes with Swift 4.1.50, I'm seeing the following error which I'm not sure how to fix:
无法为类型'Range<调用初始化程序.具有类型为((Range< String.Index>)'的参数列表的String.Index>'
Cannot invoke initializer for type 'Range< String.Index>' with an argument list of type '(Range< String.Index>)'
在Range<Index>(start..<self.endIndex)(第3行)的以下功能中:
in the following function at Range<Index>(start..<self.endIndex) (line 3):
func index(of aString: String, startingFrom position: Int? = 0) -> String.Index? { let start: String.Index = self.index(self.startIndex, offsetBy: position!) let range: Range<Index> = Range<Index>(start..<self.endIndex) return self.range(of: aString, options: .literal, range: range, locale: nil)?.lowerBound }有什么想法要解决初始化程序吗?
Any idea how to fix the initializer?
推荐答案某些背景:
在Swift 3中,引入了其他范围类型,使得 四个(例如,参见 Ole Begemann:Swift 3中的范围) :
Some background:
In Swift 3, additional range types were introduced, making a total of four (see for example Ole Begemann: Ranges in Swift 3):
Range, ClosedRange, CountableRange, CountableClosedRange使用 SE-0143有条件的实现Swift 4.2中的一致性,可数"变体 不再是单独的类型,而是(受限的)类型别名,例如
With the implementation of SE-0143 Conditional conformances in Swift 4.2, the "countable" variants are not separate types anymore, but (constrained) type aliases, for example
public typealias CountableRange<Bound: Strideable> = Range<Bound> where Bound.Stride : SignedInteger,因此,不同之间的各种转换 范围类型已被删除,例如
and, as a consequence, various conversions between the different range types have been removed, such as the
init(_ other: Range<Range.Bound>)struct Range的初始化程序.所有这些更改是 [stdlib] [WIP]使用条件一致性(#13342)消除(Closed)CountableRange 提交
initializer of struct Range. All theses changes are part of the [stdlib][WIP] Eliminate (Closed)CountableRange using conditional conformance (#13342) commit.
这就是为什么
let range: Range<Index> = Range<Index>(start..<self.endIndex)不再编译.
您已经知道,可以简单地将其固定为
As you already figured out, this can be simply fixed as
let range: Range<Index> = start..<self.endIndex或者只是
let range = start..<self.endIndex没有类型注释.
另一种选择是使用单面范围 (在Swift 4中以 SE-0172引入单边范围):
Another option is to use a one sided range (introduced in Swift 4 with SE-0172 One-sided Ranges):
extension String { func index(of aString: String, startingFrom position: Int = 0) -> String.Index? { let start = index(startIndex, offsetBy: position) return self[start...].range(of: aString, options: .literal)?.lowerBound } }之所以有效,是因为子字符串self[start...] 共享其索引 与原始字符串self.
This works because the substring self[start...] shares its indices with the originating string self.
更多推荐
无法为类型"Range< String.Index>"调用初始化程序具有类型为((Range< S
发布评论