<分区>
这在 Swift 3.2 中运行良好:
NSRange(location:myString.startIndex.distance(to: myIndex),length:myLength)
但是在 Swift 4 中它给出了标题中的错误。做什么?
标签 swift
<分区>
这在 Swift 3.2 中运行良好:
NSRange(location:myString.startIndex.distance(to: myIndex),length:myLength)
但是在 Swift 4 中它给出了标题中的错误。做什么?
最佳答案
切片字符串在 Swift 4 中发生了很大变化。NSRange
通常不再是正确的工具,所以如何解决这个问题取决于周围的其余代码。也就是说,Swift 4 中的等效代码是这样的:
NSRange(location:myString.distance(from: myString.startIndex, to: myIndex),length:myLength)
参见 String Manifesto了解 Strings future 的工作方式。参见 SE-0163和 SE-0180了解 4.0 中的更多具体变化。
关于 swift 4 : 'distance(to:)' is unavailable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50839876/