gpt4 book ai didi

ios - 没有 '...' 候选人产生 'Range'

转载 作者:行者123 更新时间:2023-11-28 19:33:33 25 4
gpt4 key购买 nike

在将旧的 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 。)

关于ios - 没有 '...' 候选人产生 'Range<String.Index>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41436034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com