distance(from:to:)' 不可用:Swift 4 中的任何字符串视图索引转换都可能失败;请打开可选索引 [英] distance(from:to:)' is unavailable: Any String view index conversion can fail in Swift 4; please unwrap the optional indices
本文介绍了distance(from:to:)' 不可用:Swift 4 中的任何字符串视图索引转换都可能失败;请打开可选索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将我的应用程序迁移到 Swift 4、Xcode 9.我收到此错误.它来自第 3 方框架.
I was trying to migrate my app to Swift 4, Xcode 9. I get this error. Its coming from a 3rd party framework.
distance(from:to:)' 不可用:Swift 4 中的任何字符串视图索引转换都可能失败;请解开可选索引
distance(from:to:)' is unavailable: Any String view index conversion can fail in Swift 4; please unwrap the optional indices
func nsRange(from range: Range<String.Index>) -> NSRange {
let utf16view = self.utf16
let from = range.lowerBound.samePosition(in: utf16view)
let to = range.upperBound.samePosition(in: utf16view)
return NSMakeRange(utf16view.distance(from: utf16view.startIndex, to: from), // Error: distance(from:to:)' is unavailable: Any String view index conversion can fail in Swift 4; please unwrap the optional indices
utf16view.distance(from: from, to: to))// Error: distance(from:to:)' is unavailable: Any String view index conversion can fail in Swift 4; please unwrap the optional indices
}
推荐答案
您可以像这样简单地解开可选索引:
You can simply unwrap the optional indices like this:
func nsRange(from range: Range<String.Index>) -> NSRange? {
let utf16view = self.utf16
if let from = range.lowerBound.samePosition(in: utf16view), let to = range.upperBound.samePosition(in: utf16view) {
return NSMakeRange(utf16view.distance(from: utf16view.startIndex, to: from), utf16view.distance(from: from, to: to))
}
return nil
}
这篇关于distance(from:to:)' 不可用:Swift 4 中的任何字符串视图索引转换都可能失败;请打开可选索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文