升级到Xcode版本10.2(10E125)后,使用range.upperBound.samePosition(in:utf16)时发生致命错误

时间:2019-03-28 03:08:30

标签: ios swift xcode swift5

升级到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(在utf16中)

错误

  

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

我希望跑步愉快

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

0 个答案:

没有答案