Cannot invoke initializer for type 'Range<String.Index>' with an argument list of type '(Range<String.Index>)'

℡╲_俬逩灬. 提交于 2019-11-27 04:30:30

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

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>)

initializer of struct Range. All theses changes are part of the [stdlib][WIP] Eliminate (Closed)CountableRange using conditional conformance (#13342) commit.

So that is the reason why

let range: Range<Index> = Range<Index>(start..<self.endIndex)

does not compile anymore.

How to fix

As you already figured out, this can be simply fixed as

let range: Range<Index> = start..<self.endIndex

or just

let range = start..<self.endIndex

without the type annotation.

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
    }
}

This works because the substring self[start...] shares its indices with the originating string self.

Turns out, ranges do not have to be initialized but can simply be created as follows:

let range: Range<Index> = start...end

In this case, the code would be fixed by replacing Range<Index>(start..<self.endIndex) with:

let range: Range<Index> = start..<self.endIndex

I had the same problem, You can use this code to fix the issue -

let range = startIndex ..< characters.index(startIndex, offsetBy: 1)

Reference : https://github.com/Ahmed-Ali/JSONExport/issues/121

I migrated from xcode 9.2 to xcode 10.1 and then started facing this error and resolved by this way

let range = start...end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!