0

升级到 Xcode 版本 10.2 (10E125) 后,出现致命错误range.upperBound.samePosition(in: utf16)

extension String {

    //Range => NSRange
    func nsRange(from range: Range<String.Index>) -> NSRange {
        guard let to = range.upperBound.samePosition(in: utf16) else {
            return NSMakeRange(0, 0)
        }

        guard let from = range.lowerBound.samePosition(in: utf16) else {
            return NSMakeRange(0, 0)
        }

        return NSMakeRange(utf16.distance(from: utf16.startIndex, to: from), utf16.distance(from: from, to: to))
    }

    //NSRange =>Range
    func range(from range: NSRange) -> Range<String.Index>? {
        guard let from16 = utf16.index(utf16.startIndex, offsetBy: range.location, limitedBy: utf16.endIndex) else { return nil }
        guard let to16 = utf16.index(from16, offsetBy: range.length, limitedBy: utf16.endIndex) else { return nil }
        guard let from = String.Index(from16, within: self) else { return nil }
        guard let to = String.Index(to16, within: self) else { return nil }
        return from ..< to
    }
}

它开始崩溃

range.upperBound.samePosition(in: utf16)

错误

线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)

我期待良好的运行

图片:[1]:https ://i.stack.imgur.com/bfbbf.png

4

0 回答 0