繁体   English   中英

没有“…”候选人产生“范围” <String.Index> &#39;

[英]No '…' candidates produce 'Range<String.Index>'

将旧的iOS应用程序转换为Sift 3.0时,遇到以下问题:代码为:

cutRange = numberString.index(numberString.startIndex, offsetBy:2)...numberString.index(numberString.startIndex, offsetBy:5)

我收到的错误消息是:

No '...' candidates produce the expected contextual result type 'Range<String.Index>' (aka 'Range<String.CharacterView.Index>')

我看到了一些与该主题相关的帖子,但并不十分满意。

那么解决这个问题的最简单方法是什么?

在Swift 3中,两个范围运算符产生不同的结果:

  • 封闭范围运算符... > ClosedRange (默认情况下)
  • (半开)范围运算符..< -> Range (默认情况下)

因此,假设您的cutRange声明为Range<String.Index> ,则需要使用半开范围运算符..<

cutRange = numberString.index(numberString.startIndex, offsetBy:2)..<numberString.index(numberString.startIndex, offsetBy:6)

(请不要错过最后一个偏移量已更改为6 。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM