无法为类型“Range<String.Index>"调用初始化程序带有类型为 '(start: String.Index, end: String.Index)'

编程入门 行业动态 更新时间:2024-10-28 11:25:45
本文介绍了无法为类型“Range<String.Index>"调用初始化程序带有类型为 '(start: String.Index, end: 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,hex 是一个字符串,但是这段代码抛出了一个错误说:

This is Swift3.0, hex is a string, but this code throws an error saying that:

无法使用类型Range"调用初始化程序'(start: String.Index, end: String.Index)' 类型的参数列表

Cannot invoke initializer for type 'Range' with an argument list of type '(start: String.Index, end: String.Index)'

推荐答案

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

Range.init(start:end:) constructor was removed in Swift 3.0 so you initialize a range like follows:

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

它返回一个 half-open 类型的 范围.然后,您可以使用它执行以下操作:

which returns a half-open range of type <String.Index>. Then, you can do the following with that:

hex.substring(with: range)

更多推荐

无法为类型“Range<String.Index>"调用初始化程序带有类型为 '(start: String.Index, end

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

发布评论

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

>www.elefans.com

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