升级到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)
我希望跑步愉快