distance(from:to:)' is unavailable: Any String view index conversion can fail in Swift 4; please unwrap the optional indices

后端 未结 3 510
死守一世寂寞
死守一世寂寞 2021-01-11 23:51

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:)\' is unavailable: Any

3条回答
  •  时光取名叫无心
    2021-01-12 00:46

    You can simply unwrap the optional indices like this:

    func nsRange(from range: Range) -> 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
    }
    

提交回复
热议问题