{"id":9311,"date":"2022-03-22T22:33:40","date_gmt":"2022-03-22T22:33:40","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9311"},"modified":"2022-03-22T22:33:40","modified_gmt":"2022-03-22T22:33:40","slug":"hey-swift-how-many-range-types-do-you-need","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/hey-swift-how-many-range-types-do-you-need\/","title":{"rendered":"Hey Swift, how many Range types do you need ?"},"content":{"rendered":"<p>I was messing around with the random number generator code for Int and noticed that the API has two Range types, Range and ClosedRange. Upon further investigation I found that Swift supports even more range types. <\/p>\n<p>So currently the range types are Range, ClosedRange, PartialRangeFrom, PartialRangeUpTo, PartialRangeThrough and CountableRange.<\/p>\n<p><strong>Range<\/strong><\/p>\n<p>The <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/range\/\" rel=\"noopener\" target=\"_blank\">Range<\/a> type is &#8220;a half-open interval from a lower bound up to, but not including, an upper bound&#8221;. Which basically means the ..&lt; syntax, i.e.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nlet range = 1..&lt;100\r\n<\/pre>\n<p>In other words <em>range<\/em> is from (and including) 1 to 100 (but not including 100).<\/p>\n<p>We might write a function to handle this using the following syntax<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/ usage rangeTest.range(1..&lt;10)\r\nfunc range(_ r: Range&lt;Int&gt;) -&gt; Void {\r\n  print(&quot;\\(r.lowerBound), \\(r.upperBound)&quot;)\r\n}\r\n<\/pre>\n<p><strong>ClosedRange<\/strong><\/p>\n<p>The <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/closedrange\/\" rel=\"noopener\" target=\"_blank\">ClosedRange<\/a> type is &#8220;an interval from a lower bound up to, and including, an upper bound&#8221;. Which basically means the &#8230; syntax, i.e.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nlet range = 1...100\r\n<\/pre>\n<p>In other words <em>range<\/em> is from (and including) 1 to 100 (and including 100).<\/p>\n<p>We might write a function to handle this using the following syntax<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/ rangeTest.range(1...10)\r\nfunc range(_ r: ClosedRange&lt;Int&gt;) -&gt; Void {\r\n  print(&quot;\\(r.lowerBound), \\(r.upperBound)&quot;)\r\n}\r\n<\/pre>\n<p><strong>PartialRangeFrom<\/strong><\/p>\n<p>The <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/PartialRangeFrom\/\" rel=\"noopener\" target=\"_blank\">PartialRangeFrom<\/a> type is &#8220;a partial interval extending upward from a lower bound&#8221;. Which uses &#8230; syntax, i.e.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nlet range = 0...\r\n<\/pre>\n<p>We might write a function to handle this using the following syntax<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/ rangeTest.range(..&lt;10)\r\nfunc range(_ r: PartialRangeUpTo&lt;Int&gt;) -&gt; Void {\r\n  print(&quot;\\(r.lowerBound)&quot;)\r\n}\r\n<\/pre>\n<p><strong>PartialRangeUpTo<\/strong><\/p>\n<p>The <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/PartialRangeUpTo\/\" rel=\"noopener\" target=\"_blank\">PartialRangeUpTo<\/a> type is &#8220;a partial half-open interval up to, but not including, an upper bound&#8221;. Which uses ..&lt; syntax, i.e.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nlet range = ..&lt;100\r\n<\/pre>\n<p>We might write a function to handle this using the following syntax<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/ rangeTest.range(..&lt;10)\r\nfunc range(_ r: PartialRangeUpTo&lt;Int&gt;) -&gt; Void {\r\n  print(&quot;\\(r.upperBound)&quot;)\r\n}\r\n<\/pre>\n<p><strong>PartialRangeThrough<\/strong><\/p>\n<p>The <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/PartialRangeThrough\/\" rel=\"noopener\" target=\"_blank\">PartialRangeThrough<\/a> type is &#8220;a partial interval up to, and including, an upper bound&#8221;. Which uses the &#8230; syntax, i.e.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nlet range = ...100\r\n<\/pre>\n<p>We might write a function to handle this using the following syntax<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/ rangeTest.range(...10)\r\nfunc range(_ r: PartialRangeThrough&lt;Int&gt;) -&gt; Void {\r\n  print(&quot;\\(r.upperBound)&quot;)\r\n}\r\n<\/pre>\n<p><strong>CountableRange<\/strong><\/p>\n<p>The <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/CountableRange\/\" rel=\"noopener\" target=\"_blank\">CountableRange<\/a> is just a type alias around a Range<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was messing around with the random number generator code for Int and noticed that the API has two Range types, Range and ClosedRange. Upon further investigation I found that Swift supports even more range types. So currently the range types are Range, ClosedRange, PartialRangeFrom, PartialRangeUpTo, PartialRangeThrough and CountableRange. Range The Range type is &#8220;a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[286],"tags":[],"class_list":["post-9311","post","type-post","status-publish","format-standard","hentry","category-swift"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9311"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9311\/revisions"}],"predecessor-version":[{"id":9318,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9311\/revisions\/9318"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}