无法为类型"Range< String.Index>"调用初始化程序具有类型为((Range< String.Index>)

编程入门 行业动态 更新时间:2024-10-27 19:28:02
本文介绍了无法为类型"Range< String.Index>"调用初始化程序具有类型为((Range< String.Index>))的参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在更新到显然是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&lt; String.Index&gt;"调用初始化程序具有类型为((Range&lt; S

本文发布于:2023-11-26 16:36:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634399.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   初始化   参数   程序   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!