{"id":16660,"date":"2016-12-19T09:46:24","date_gmt":"2016-12-19T14:46:24","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=16660"},"modified":"2016-12-19T09:46:24","modified_gmt":"2016-12-19T14:46:24","slug":"nsregularexpression-and-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/12\/19\/nsregularexpression-and-swift\/","title":{"rendered":"NSRegularExpression and Swift"},"content":{"rendered":"<p><a href=\"http:\/\/nshipster.com\/nsregularexpression\/\">Nate Cook<\/a>:<\/p>\n<blockquote cite=\"http:\/\/nshipster.com\/nsregularexpression\/\">\n<p>Well, many <code>NSRegularExpression<\/code> methods use <code>NSRange<\/code>s, as do the <code>NSTextCheckingResult<\/code> instances that store a match&rsquo;s data. <code>NSRange<\/code>, in turn, uses integers for its location and length, while none of <code>String<\/code>&rsquo;s views use integers as an index[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>With that in mind, here are a few additions to String that will make straddling the Swift\/Objective-C divide a bit easier:<\/p>\n<pre>extension String {\n    \/\/\/ An `NSRange` that represents the full range of the string.\n    var nsrange: NSRange {\n        return NSRange(location: 0, length: utf16.count)\n    }\n\n    \/\/\/ Returns a substring with the given `NSRange`, \n    \/\/\/ or `nil` if the range can't be converted.\n    func substring(with nsrange: NSRange) -&gt; String? {\n        guard let range = nsrange.toRange() \n            else { return nil }\n        let start = UTF16Index(range.lowerBound)\n        let end = UTF16Index(range.upperBound)\n        return String(utf16[start..&lt;end])\n    }\n\n    \/\/\/ Returns a range equivalent to the given `NSRange`,\n    \/\/\/ or `nil` if the range can't be converted.\n    func range(from nsrange: NSRange) -&gt; Range&lt;Index&gt;? {\n        guard let range = nsrange.toRange() else { return nil }\n        let utf16Start = UTF16Index(range.lowerBound)\n        let utf16End = UTF16Index(range.upperBound)\n\n        guard let start = Index(utf16Start, within: self),\n            let end = Index(utf16End, within: self)\n            else { return nil }\n\n        return start..&lt;end\n    }\n}<\/pre>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Nate Cook: Well, many NSRegularExpression methods use NSRanges, as do the NSTextCheckingResult instances that store a match&rsquo;s data. NSRange, in turn, uses integers for its location and length, while none of String&rsquo;s views use integers as an index[&#8230;] [&#8230;] With that in mind, here are a few additions to String that will make straddling the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,31,1380,30,1381,71,234,901,258],"class_list":["post-16660","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-ios-10","tag-mac","tag-macos-10-12","tag-programming","tag-regex","tag-swift-programming-language","tag-unicode"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=16660"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16660\/revisions"}],"predecessor-version":[{"id":16661,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16660\/revisions\/16661"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=16660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=16660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=16660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}