无法为类型“Range<String.Index>"调用初始化程序带有类型为 '(start: String.Index, end: String.Index)' 的参数列表 [英] Cannot invoke initializer for type &#39;Range&lt;String.Index&gt;&#39; with an argument list of type &#39;(start: String.Index, end: String.Index)&#39;

查看:42
本文介绍了无法为类型“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: String.Index)' 的参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆