NSRegularExpression and Swift
Well, many
NSRegularExpression
methods useNSRange
s, as do theNSTextCheckingResult
instances that store a match’s data.NSRange
, in turn, uses integers for its location and length, while none ofString
’s views use integers as an index[…][…]
With that in mind, here are a few additions to String that will make straddling the Swift/Objective-C divide a bit easier:
extension String { /// An `NSRange` that represents the full range of the string. var nsrange: NSRange { return NSRange(location: 0, length: utf16.count) } /// Returns a substring with the given `NSRange`, /// or `nil` if the range can't be converted. func substring(with nsrange: NSRange) -> String? { guard let range = nsrange.toRange() else { return nil } let start = UTF16Index(range.lowerBound) let end = UTF16Index(range.upperBound) return String(utf16[start..<end]) } /// Returns a range equivalent to the given `NSRange`, /// or `nil` if the range can't be converted. func range(from nsrange: NSRange) -> Range<Index>? { guard let range = nsrange.toRange() else { return nil } let utf16Start = UTF16Index(range.lowerBound) let utf16End = UTF16Index(range.upperBound) guard let start = Index(utf16Start, within: self), let end = Index(utf16End, within: self) else { return nil } return start..<end } }
2 Comments RSS · Twitter
December 29, 2018 2:45 AM
[…] (a través de: https://mjtsai.com/blog/2016/12/19/nsregularexpression-and-swift/) […]