<?xml version="1.0" encoding="utf-8" standalone="yes"?><feed version="2.0" xmlns="http://www.w3.org/2005/Atom"><title>Code - Cube Free Coder</title><title/><link rel="alternate" type="text/html" language="en" href="https://cubefreecoder.com/categories/code/"/><link rel="self" type="application/octet-stream" href="/index.xml"/><subtitle>Recent content in Code on Cube Free Coder</subtitle><id>https://cubefreecoder.com/categories/code/</id><generator>Hugo -- gohugo.io</generator><rights>&lt;a href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank" rel="noopener"&gt;CC BY-NC 4.0&lt;/a&gt;</rights><updated>2026-01-22T00:00:00Z</updated><entry><title>Editing multiple occurrances in Xcode (with Vim)</title><link rel="alternate" type="text/html" href="https://cubefreecoder.com/posts/vim-multiple-edits/"/><published>2026-01-22T00:00:00Z</published><updated>2026-01-22T00:00:00Z</updated><summary>&lt;p&gt;Here is a real gem of a tip burried deep within the &lt;a href="https://developer.apple.com/videos/play/wwdc2024/10169?time=1812"&gt;Swift 6 migration video&lt;/a&gt; to edit multiple occurances of a word/keyword in one fell swoop.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Select the text&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If you are sitting at the first character in the word, use &lt;code&gt;ve&lt;/code&gt; to select to the end of the word.&lt;/li&gt;
&lt;li&gt;If you are in the middle of the word, &lt;code&gt;viw&lt;/code&gt; will select the entire thing.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Press Cmd+Opt+e to select the next occurance.&lt;/p&gt;</summary><content type="html"><![CDATA[<p>Here is a real gem of a tip burried deep within the <a href="https://developer.apple.com/videos/play/wwdc2024/10169?time=1812">Swift 6 migration video</a> to edit multiple occurances of a word/keyword in one fell swoop.</p>
<ol>
<li>
<p>Select the text</p>
<ul>
<li>If you are sitting at the first character in the word, use <code>ve</code> to select to the end of the word.</li>
<li>If you are in the middle of the word, <code>viw</code> will select the entire thing.</li>
</ul>
</li>
<li>
<p>Press Cmd+Opt+e to select the next occurance.</p>
<ul>
<li>This can be repeated as many times as necessary.</li>
</ul>
</li>
<li>
<p>Type <code>c</code> and make the desired change.</p>
</li>
</ol>
]]></content><id>https://cubefreecoder.com/posts/vim-multiple-edits/</id><author><name>David Bireta</name></author></entry><entry><title>Querying SwiftData with a date-based Predicate</title><link rel="alternate" type="text/html" href="https://cubefreecoder.com/posts/swiftdata-predicate-matching-date/"/><published>2026-01-20T00:00:00Z</published><updated>2026-01-20T00:00:00Z</updated><summary>&lt;p&gt;I was working on a project where I needed to query items from swift data that occurred on a specific date. When dealing with dates, my first instinct is usually to reach for the &lt;code&gt;Calendar&lt;/code&gt; object to do all the heavy lifting for me. This lead me to have something like:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-swift" data-lang="swift"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;fixturesHappeningToday&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;LocalFixture&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;predicate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="n"&gt;Predicate&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;LocalFixture&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isDateInToday&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;descriptor&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;FetchDescriptor&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;LocalFixture&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;predicate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;results&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;modelContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;descriptor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="p"&gt;??&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Compiler error:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// The isDateInToday(_:) function is not supported in this predicate&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br&gt;
&lt;p&gt;That didn&amp;rsquo;t work out, and it kind of makes sense since the Predicate needs to be converted to SQL eventually. So instead I pivoted and tried to just format the values and compare them.&lt;/p&gt;</summary><content type="html"><![CDATA[<p>I was working on a project where I needed to query items from swift data that occurred on a specific date. When dealing with dates, my first instinct is usually to reach for the <code>Calendar</code> object to do all the heavy lifting for me. This lead me to have something like:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">private</span> <span class="kd">func</span> <span class="nf">fixturesHappeningToday</span><span class="p">()</span> <span class="p">-&gt;</span> <span class="p">[</span><span class="n">LocalFixture</span><span class="p">]</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">let</span> <span class="nv">predicate</span> <span class="p">=</span> <span class="p">#</span><span class="n">Predicate</span><span class="p">&lt;</span><span class="n">LocalFixture</span><span class="p">&gt;</span> <span class="p">{</span> <span class="n">item</span> <span class="k">in</span>
</span></span><span class="line"><span class="cl">        <span class="n">Calendar</span><span class="p">.</span><span class="n">current</span><span class="p">.</span><span class="n">isDateInToday</span><span class="p">(</span><span class="n">item</span><span class="p">.</span><span class="n">date</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="kd">let</span> <span class="nv">descriptor</span> <span class="p">=</span> <span class="n">FetchDescriptor</span><span class="p">&lt;</span><span class="n">LocalFixture</span><span class="p">&gt;(</span><span class="n">predicate</span><span class="p">:</span> <span class="n">predicate</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="kd">let</span> <span class="nv">results</span> <span class="p">=</span> <span class="k">try</span><span class="p">?</span> <span class="n">modelContext</span><span class="p">.</span><span class="n">fetch</span><span class="p">(</span><span class="n">descriptor</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="k">return</span> <span class="n">results</span> <span class="p">??</span> <span class="p">[]</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// Compiler error:</span>
</span></span><span class="line"><span class="cl"><span class="c1">// The isDateInToday(_:) function is not supported in this predicate</span>
</span></span></code></pre></div><br>
<p>That didn&rsquo;t work out, and it kind of makes sense since the Predicate needs to be converted to SQL eventually. So instead I pivoted and tried to just format the values and compare them.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">predicate</span> <span class="p">=</span> <span class="p">#</span><span class="n">Predicate</span><span class="p">&lt;</span><span class="n">LocalFixture</span><span class="p">&gt;</span> <span class="p">{</span> <span class="n">item</span> <span class="k">in</span>
</span></span><span class="line"><span class="cl">    <span class="n">item</span><span class="p">.</span><span class="n">date</span><span class="p">.</span><span class="n">formatted</span><span class="p">(</span><span class="n">date</span><span class="p">:</span> <span class="p">.</span><span class="n">numeric</span><span class="p">,</span> <span class="n">time</span><span class="p">:</span> <span class="p">.</span><span class="n">omitted</span><span class="p">)</span> <span class="p">==</span> <span class="n">Date</span><span class="p">.</span><span class="n">now</span><span class="p">.</span><span class="n">formatted</span><span class="p">(</span><span class="n">date</span><span class="p">:</span> <span class="p">.</span><span class="n">numeric</span><span class="p">,</span> <span class="n">time</span><span class="p">:</span> <span class="p">.</span><span class="n">omitted</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// Compiler error:</span>
</span></span><span class="line"><span class="cl"><span class="c1">// The formatted(date:time:) function is not supported in this predicate</span>
</span></span></code></pre></div><p>Yep, pretty much the same compiler error. I probably should have guessed that the <code>formatted</code> function would have the same limitation, but it was worth a shot.</p>
<p>At one point I considered storing pieces of the date values, but I didn&rsquo;t like the idea of having duplicate data. The next idea was to create a computed, transient property. This actually compiled! Hooray.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"> <span class="kd">var</span> <span class="nv">dayMonthYear</span><span class="p">:</span> <span class="nb">String</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">     <span class="n">date</span><span class="p">.</span><span class="n">formatted</span><span class="p">(.</span><span class="n">dateTime</span><span class="p">.</span><span class="n">day</span><span class="p">().</span><span class="n">month</span><span class="p">().</span><span class="n">year</span><span class="p">())</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span><span class="line"><span class="cl"> 
</span></span><span class="line"><span class="cl"> <span class="kd">let</span> <span class="nv">predicate</span> <span class="p">=</span> <span class="p">#</span><span class="n">Predicate</span><span class="p">&lt;</span><span class="n">LocalFixture</span><span class="p">&gt;</span> <span class="p">{</span> <span class="n">item</span> <span class="k">in</span>
</span></span><span class="line"><span class="cl">     <span class="n">fixture</span><span class="p">.</span><span class="n">dayMonthYear</span> <span class="p">==</span> <span class="n">match</span>
</span></span><span class="line"><span class="cl"> <span class="p">}</span>
</span></span></code></pre></div><p>And then promptly crashed when the app was run.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="c1">// Fatal error: Couldn&#39;t find \LocalFixture.dayMonthYear on LocalFixture with fields dayMonthYear</span>
</span></span></code></pre></div><p>At this point, I thought perhaps I was just overthinking all of this and should just compare the dates directly.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">targetDate</span> <span class="p">=</span> <span class="n">Date</span><span class="p">.</span><span class="n">now</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">predicate</span> <span class="p">=</span> <span class="p">#</span><span class="n">Predicate</span><span class="p">&lt;</span><span class="n">LocalFixture</span><span class="p">&gt;</span> <span class="p">{</span> <span class="n">item</span> <span class="k">in</span>
</span></span><span class="line"><span class="cl">    <span class="n">item</span><span class="p">.</span><span class="n">date</span> <span class="p">==</span> <span class="n">targetDate</span>     <span class="c1">// Can&#39;t use Date.now directly in #Predicate</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>No crash this time, but I didn&rsquo;t get any results back. After a few minutes of head scratching it dawned on me that this was of course comparing the full date value, including the time. After a stretch, and a quick walk away from my desk, I finally hit on an idea that worked. Utilize the <code>Calendar</code> to create start and end dates, and then do simple compares against those directly.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">start</span> <span class="p">=</span> <span class="n">Calendar</span><span class="p">.</span><span class="n">current</span><span class="p">.</span><span class="n">startOfDay</span><span class="p">(</span><span class="k">for</span><span class="p">:</span> <span class="n">targetDate</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">end</span> <span class="p">=</span> <span class="n">Calendar</span><span class="p">.</span><span class="n">current</span><span class="p">.</span><span class="n">date</span><span class="p">(</span><span class="n">byAdding</span><span class="p">:</span> <span class="p">.</span><span class="n">day</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="n">to</span><span class="p">:</span> <span class="n">start</span><span class="p">)</span><span class="o">!</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">predicate</span> <span class="p">=</span> <span class="p">#</span><span class="n">Predicate</span><span class="p">&lt;</span><span class="n">LocalFixture</span><span class="p">&gt;</span> <span class="p">{</span> <span class="n">item</span> <span class="k">in</span>
</span></span><span class="line"><span class="cl">    <span class="n">item</span><span class="p">.</span><span class="n">date</span> <span class="o">&gt;</span> <span class="n">start</span> <span class="o">&amp;&amp;</span> <span class="n">item</span><span class="p">.</span><span class="n">date</span> <span class="o">&lt;</span> <span class="n">end</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>We can&rsquo;t really perform any operations on <code>item.date</code>, but we CAN do some pre-calculations to make it easier to compare against. Using theses <code>Calendar</code> methods we can safely create a range from midnight to midnight, fully encapsulating our target date.</p>
]]></content><id>https://cubefreecoder.com/posts/swiftdata-predicate-matching-date/</id><author><name>David Bireta</name></author></entry><entry><title>Swift Calendar favorites</title><link rel="alternate" type="text/html" href="https://cubefreecoder.com/posts/calendar-props/"/><published>2025-12-03T00:00:00Z</published><updated>2025-12-03T00:00:00Z</updated><summary>&lt;p&gt;The &lt;code&gt;Calendar&lt;/code&gt; class in Swift has so many useful helper methods to make working with dates easier, and less error prone. A couple of my absolute favorites:&lt;/p&gt;
&lt;h2 id="isdateintoday_-date-date---bool"&gt;isDateInToday(_ date: Date) -&amp;gt; Bool&lt;/h2&gt;
&lt;p&gt;Simple and to the point.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-swift" data-lang="swift"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;occursToday&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Calendar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isDateInToday&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;someDate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="isdate_-date1-date-insamedayas-date2-date---bool"&gt;isDate(_ date1: Date, inSameDayAs date2: Date) -&amp;gt; Bool&lt;/h2&gt;
&lt;p&gt;Say you have two dates returned from a server as UNIX timestamps, and want to check if they represent the same date. You could parse them into date componenets (which is another great feature of &lt;code&gt;Calendar&lt;/code&gt;), and compare the year, month, and day. Or use the simple one-liner:&lt;/p&gt;</summary><content type="html"><![CDATA[<p>The <code>Calendar</code> class in Swift has so many useful helper methods to make working with dates easier, and less error prone. A couple of my absolute favorites:</p>
<h2 id="isdateintoday_-date-date---bool">isDateInToday(_ date: Date) -&gt; Bool</h2>
<p>Simple and to the point.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">occursToday</span> <span class="p">=</span> <span class="n">Calendar</span><span class="p">.</span><span class="n">current</span><span class="p">.</span><span class="n">isDateInToday</span><span class="p">(</span><span class="n">someDate</span><span class="p">)</span>
</span></span></code></pre></div><h2 id="isdate_-date1-date-insamedayas-date2-date---bool">isDate(_ date1: Date, inSameDayAs date2: Date) -&gt; Bool</h2>
<p>Say you have two dates returned from a server as UNIX timestamps, and want to check if they represent the same date. You could parse them into date componenets (which is another great feature of <code>Calendar</code>), and compare the year, month, and day. Or use the simple one-liner:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">date</span> <span class="p">=</span> <span class="n">Date</span><span class="p">(</span><span class="n">timeIntervalSince1970</span><span class="p">:</span> <span class="mi">1764636075</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">anotherDate</span> <span class="p">=</span> <span class="n">Date</span><span class="p">(</span><span class="n">timeIntervalSince1970</span><span class="p">:</span> <span class="mi">1764596475</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">isTheSameDay</span> <span class="p">=</span> <span class="n">Calendar</span><span class="p">.</span><span class="n">current</span><span class="p">.</span><span class="n">isDate</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">inSameDayAs</span><span class="p">:</span> <span class="n">anotherDate</span><span class="p">)</span>
</span></span></code></pre></div><p>It&rsquo;s just as easy to resolve the match to a given hour, month, or any other component.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">isInTheSameHour</span> <span class="p">=</span> <span class="n">Calendar</span><span class="p">.</span><span class="n">current</span><span class="p">.</span><span class="n">isDate</span><span class="p">(</span><span class="n">date</span><span class="p">,</span> <span class="n">equalTo</span><span class="p">:</span> <span class="n">anotherDate</span><span class="p">,</span> <span class="n">toGranularity</span><span class="p">:</span> <span class="p">.</span><span class="n">hour</span><span class="p">)</span>
</span></span></code></pre></div>]]></content><id>https://cubefreecoder.com/posts/calendar-props/</id><author><name>David Bireta</name></author></entry><entry><title>Advancing Date values in Swift</title><link rel="alternate" type="text/html" href="https://cubefreecoder.com/posts/moving-through-time/"/><published>2024-06-28T00:00:00Z</published><updated>2024-06-28T00:00:00Z</updated><summary>&lt;p&gt;Swift makes it easy to advance a &lt;code&gt;Date&lt;/code&gt; value by a given number of seconds. Just use the &lt;code&gt;.addingTimeInterval()&lt;/code&gt; method.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-swift" data-lang="swift"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;date&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;sixtySecondsLater&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;addingTimeInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;twentyMinutesLater&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;addingTimeInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Negative values also work just fine&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;sixtySecondsAgo&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;addingTimeInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br&gt;
&lt;p&gt;For a more readable, and less error-prone approach, use the &lt;code&gt;Calendar&lt;/code&gt; method of &lt;code&gt;date(byAdding:…)&lt;/code&gt;. You can easily and explicitly specify the date component you want to move forward (or backwards) by, as well as how much. There is an additional overload of the method which accepts a full &lt;code&gt;DateComponents&lt;/code&gt; object, which allows you to easily change multiple components in one go.&lt;/p&gt;</summary><content type="html"><![CDATA[<p>Swift makes it easy to advance a <code>Date</code> value by a given number of seconds. Just use the <code>.addingTimeInterval()</code> method.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">date</span> <span class="p">=</span> <span class="n">Date</span><span class="p">.</span><span class="n">now</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">sixtySecondsLater</span> <span class="p">=</span> <span class="n">date</span><span class="p">.</span><span class="n">addingTimeInterval</span><span class="p">(</span><span class="mi">60</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">twentyMinutesLater</span> <span class="p">=</span> <span class="n">date</span><span class="p">.</span><span class="n">addingTimeInterval</span><span class="p">(</span><span class="mi">60</span> <span class="o">*</span> <span class="mi">20</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// Negative values also work just fine</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">sixtySecondsAgo</span> <span class="p">=</span> <span class="n">date</span><span class="p">.</span><span class="n">addingTimeInterval</span><span class="p">(</span><span class="o">-</span><span class="mi">60</span><span class="p">)</span>
</span></span></code></pre></div><br>
<p>For a more readable, and less error-prone approach, use the <code>Calendar</code> method of <code>date(byAdding:…)</code>. You can easily and explicitly specify the date component you want to move forward (or backwards) by, as well as how much. There is an additional overload of the method which accepts a full <code>DateComponents</code> object, which allows you to easily change multiple components in one go.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">date</span> <span class="p">=</span> <span class="n">Date</span><span class="p">.</span><span class="n">now</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">oneHourLater</span> <span class="p">=</span> <span class="n">Calendar</span><span class="p">.</span><span class="n">current</span><span class="p">.</span><span class="n">date</span><span class="p">(</span><span class="n">byAdding</span><span class="p">:</span> <span class="p">.</span><span class="n">hour</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="n">to</span><span class="p">:</span> <span class="n">date</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">twentyHoursLater</span> <span class="p">=</span> <span class="n">Calendar</span><span class="p">.</span><span class="n">current</span><span class="p">.</span><span class="n">date</span><span class="p">(</span><span class="n">byAdding</span><span class="p">:</span> <span class="p">.</span><span class="n">hour</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="mi">20</span><span class="p">,</span> <span class="n">to</span><span class="p">:</span> <span class="n">date</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">components</span> <span class="p">=</span> <span class="n">DateComponents</span><span class="p">(</span><span class="n">hour</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="n">minute</span><span class="p">:</span> <span class="mi">20</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">threeHoursTwentyMinutesLater</span> <span class="p">=</span> <span class="n">Calendar</span><span class="p">.</span><span class="n">current</span><span class="p">.</span><span class="n">date</span><span class="p">(</span><span class="n">byAdding</span><span class="p">:</span> <span class="n">components</span><span class="p">,</span> <span class="n">to</span><span class="p">:</span> <span class="n">date</span><span class="p">)</span>
</span></span></code></pre></div><br>
<p>The <code>.date(byAdding:…)</code> method has one last trick up its sleve. Say you are building a clock component where you can individually step the hours, minutes, or seconds up and down. If you would like the behavior to be such that incrementing the seconds from 55 to 5 to <em>not</em> make a change to the value of the minutes, you can set the <code>wrappingComponents</code> paramter to <code>true</code>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">date</span> <span class="p">=</span> <span class="n">Date</span><span class="p">.</span><span class="n">now</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">timeIsAnIllusion</span> <span class="p">=</span> <span class="n">Calendar</span><span class="p">.</span><span class="n">current</span><span class="p">.</span><span class="n">date</span><span class="p">(</span><span class="n">byAdding</span><span class="p">:</span> <span class="p">.</span><span class="n">second</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="mi">60</span><span class="p">,</span> <span class="n">to</span><span class="p">:</span> <span class="n">date</span><span class="p">,</span> <span class="n">wrappingComponents</span><span class="p">:</span> <span class="kc">true</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="bp">print</span><span class="p">(</span><span class="n">date</span> <span class="p">==</span> <span class="n">timeIsAnIllusion</span><span class="p">)</span>     <span class="c1">// `true`</span>
</span></span></code></pre></div>]]></content><id>https://cubefreecoder.com/posts/moving-through-time/</id><author><name>David Bireta</name></author></entry><entry><title>Location validation in Swift</title><link rel="alternate" type="text/html" href="https://cubefreecoder.com/posts/location-location-location/"/><published>2024-02-09T00:00:00Z</published><updated>2024-02-09T00:00:00Z</updated><summary>&lt;p&gt;Swift makes it super easy to create a location. Just give it your latitude and longitude:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-swift" data-lang="swift"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;someLocation&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CLLocation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;latitude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;longitude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Easy! Too easy? The slight catch here is that there is no validation being done. You can create a completely invalid location and Swift won&amp;rsquo;t complain at all. No compiler errors, no runtime errors. If you log the bad location, you can see the bad values are still maintained:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-swift" data-lang="swift"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;badLocation&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CLLocation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;latitude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;9000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;longitude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;The location is: &lt;/span&gt;&lt;span class="si"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;badLocation&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="o"&gt;---&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// The location is: &amp;lt;+9000.00000000,-9000.00000000&amp;gt; +/- 0.00m (speed -1.00 mps / course -1.00) @ 2/9/24, 13:46:53 Central Standard Time&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br/&gt;
So we probably want to make sure the location is valid. That is, check that the latitude is greater than -180 and less than 180. Or was it less than or equal... shoot, are the bounds of 180 for latitude or longitude?
&lt;p&gt;Luckily this is something we don&amp;rsquo;t have to remember. We can use the &lt;code&gt;CLLocationCoordinate2DIsValid&lt;/code&gt; method. It takes a coordinate and returns a boolean indicating if the coordinate is valid.&lt;/p&gt;</summary><content type="html"><![CDATA[<p>Swift makes it super easy to create a location. Just give it your latitude and longitude:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">someLocation</span> <span class="p">=</span> <span class="n">CLLocation</span><span class="p">(</span><span class="n">latitude</span><span class="p">:</span> <span class="mi">40</span><span class="p">,</span> <span class="n">longitude</span><span class="p">:</span> <span class="o">-</span><span class="mi">40</span><span class="p">)</span>
</span></span></code></pre></div><p>Easy! Too easy? The slight catch here is that there is no validation being done. You can create a completely invalid location and Swift won&rsquo;t complain at all. No compiler errors, no runtime errors. If you log the bad location, you can see the bad values are still maintained:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">badLocation</span> <span class="p">=</span> <span class="n">CLLocation</span><span class="p">(</span><span class="n">latitude</span><span class="p">:</span> <span class="mi">9000</span><span class="p">,</span> <span class="n">longitude</span><span class="p">:</span> <span class="o">-</span><span class="mi">9000</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="n">Logger</span><span class="p">().</span><span class="n">debug</span><span class="p">(</span><span class="s">&#34;The location is: </span><span class="si">\(</span><span class="n">badLocation</span><span class="si">)</span><span class="s">&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="o">---</span>
</span></span><span class="line"><span class="cl"><span class="c1">// The location is: &lt;+9000.00000000,-9000.00000000&gt; +/- 0.00m (speed -1.00 mps / course -1.00) @ 2/9/24, 13:46:53 Central Standard Time</span>
</span></span></code></pre></div><br/>
So we probably want to make sure the location is valid. That is, check that the latitude is greater than -180 and less than 180. Or was it less than or equal... shoot, are the bounds of 180 for latitude or longitude?  
<p>Luckily this is something we don&rsquo;t have to remember. We can use the <code>CLLocationCoordinate2DIsValid</code> method. It takes a coordinate and returns a boolean indicating if the coordinate is valid.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">questionableCoordinate</span> <span class="p">=</span> <span class="n">CLLocationCoordinate2D</span><span class="p">(</span><span class="n">latitude</span><span class="p">:</span> <span class="o">-</span><span class="mi">100</span><span class="p">,</span> <span class="n">longitude</span><span class="p">:</span> <span class="mi">95</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">isValidCoordinate</span> <span class="p">=</span> <span class="n">CLLocationCoordinate2DIsValid</span><span class="p">(</span><span class="n">questionableCoordinate</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="bp">print</span><span class="p">(</span><span class="n">isValidCoordinate</span><span class="p">)</span>    <span class="c1">// spoiler - it&#39;s not</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// Note the method is for a `CLLocationCoordinate2D`, not a `CLLocation`</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">questionableLocation</span> <span class="p">=</span> <span class="n">CLLocation</span><span class="p">(</span><span class="n">latitude</span><span class="p">:</span> <span class="o">-</span><span class="mi">100</span><span class="p">,</span> <span class="n">longitude</span><span class="p">:</span> <span class="mi">95</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="kd">let</span> <span class="nv">isValidLocation</span> <span class="p">=</span> <span class="n">CLLocationCoordinate2DIsValid</span><span class="p">(</span><span class="n">questionableLocation</span><span class="p">.</span><span class="n">coordinate</span><span class="p">)</span>
</span></span></code></pre></div><p>Now you can go on your way with confidence.</p>
<blockquote>
<p>Quick aside: When adding an annotation with an invalid coordinate to a SwiftUI Map view I&rsquo;ve seen couple of different behaviors. Most of the time the annotation just doesn&rsquo;t show up at all, which is probably the desirable behavior. Occassionally though, the framework seems to transform the invalid coordinate to one at (0,0) - the popular vacation destination of <a href="https://en.wikipedia.org/wiki/Null_Island">Null Island</a>.</p>
</blockquote>
]]></content><id>https://cubefreecoder.com/posts/location-location-location/</id><author><name>David Bireta</name></author></entry><entry><title>Manually installing the iOS 17 simulator runtime for Xcode 15 beta</title><link rel="alternate" type="text/html" href="https://cubefreecoder.com/posts/manually-install-ios17-simulator/"/><published>2023-06-05T00:00:00Z</published><updated>2023-06-05T00:00:00Z</updated><summary>&lt;p&gt;If you just downloaded the Xcode 15 beta from the Apple Developer website, you might have ended up with two files:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Xcode_15_beta.xip&lt;/code&gt; and &lt;code&gt;iOS_17_beta_Simmulator_Runtime.dmg&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;If you launch Xcode you&amp;rsquo;ll see that the iOS 17 simulator is still missing, and you won&amp;rsquo;t be able to build and run with it quite yet. The good news is that it is easy to manually import the simulator runtime, and you don&amp;rsquo;t need to download it again via Xcode&amp;rsquo;s organizer.&lt;/p&gt;</summary><content type="html"><![CDATA[<p>If you just downloaded the Xcode 15 beta from the Apple Developer website, you might have ended up with two files:</p>
<p><code>Xcode_15_beta.xip</code> and <code>iOS_17_beta_Simmulator_Runtime.dmg</code></p>
<p>If you launch Xcode you&rsquo;ll see that the iOS 17 simulator is still missing, and you won&rsquo;t be able to build and run with it quite yet. The good news is that it is easy to manually import the simulator runtime, and you don&rsquo;t need to download it again via Xcode&rsquo;s organizer.</p>
<p>Just follow these steps:</p>
<ol>
<li>In Xcode, go to Settings, Locations, and make sure the Command Line Tools is set to <code>Xcode-beta 15.0</code></li>
<li>On the command line run <code>xcrun simctl runtime add ~/Downloads/iOS_17_beta_Simulator_Runtime.dmg</code></li>
<li>After a brief time you should get a response that the simulator is ready.</li>
</ol>
<p>For more details, check out the <a href="https://developer.apple.com/documentation/xcode/installing-additional-simulator-runtimes#Install-and-manage-Simulator-runtimes-from-the-command-line">Apple documentation here.</a></p>
]]></content><id>https://cubefreecoder.com/posts/manually-install-ios17-simulator/</id><author><name>David Bireta</name></author></entry><entry><title>backgroundTask modifier crashing Swift compiler</title><link rel="alternate" type="text/html" href="https://cubefreecoder.com/posts/background-task-compiler-error/"/><published>2022-10-10T00:00:00Z</published><updated>2022-10-10T00:00:00Z</updated><summary>&lt;p&gt;As of Xcode 14.0, adding a &lt;code&gt;backgroundTask&lt;/code&gt; modifier to anything more than a single view will likely cause a not super-helpful compiler error.&lt;/p&gt;
&lt;p&gt;Say you have something like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-swift" data-lang="swift"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;WeatherApp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;App&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="n"&gt;StateObject&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;weatherData&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;WeatherDataStore&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;some&lt;/span&gt; &lt;span class="n"&gt;Scene&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;WindowGroup&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;SimpleWeatherView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environmentObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weatherData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;backgroundTask&lt;/span&gt;&lt;span class="p"&gt;(.&lt;/span&gt;&lt;span class="n"&gt;appRefresh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;WeatherCheck&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;await&lt;/span&gt; &lt;span class="n"&gt;updateWeatherData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Xcode will not be happy, letting you know that &lt;code&gt;Command CompileSwift failed with a nonzero exit code&lt;/code&gt;. Not exactly the most helpful, though digging in to the build output yields this gem:&lt;/p&gt;</summary><content type="html"><![CDATA[<p>As of Xcode 14.0, adding a <code>backgroundTask</code> modifier to anything more than a single view will likely cause a not super-helpful compiler error.</p>
<p>Say you have something like this:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="p">@</span><span class="n">main</span>
</span></span><span class="line"><span class="cl"><span class="kd">struct</span> <span class="nc">WeatherApp</span><span class="p">:</span> <span class="n">App</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="p">@</span><span class="n">StateObject</span> <span class="kd">var</span> <span class="nv">weatherData</span> <span class="p">=</span> <span class="n">WeatherDataStore</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">    
</span></span><span class="line"><span class="cl">    <span class="kd">var</span> <span class="nv">body</span><span class="p">:</span> <span class="n">some</span> <span class="n">Scene</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">WindowGroup</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">SimpleWeatherView</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">                <span class="p">.</span><span class="n">environmentObject</span><span class="p">(</span><span class="n">weatherData</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="p">.</span><span class="n">backgroundTask</span><span class="p">(.</span><span class="n">appRefresh</span><span class="p">(</span><span class="s">&#34;WeatherCheck&#34;</span><span class="p">))</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">await</span> <span class="n">updateWeatherData</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>Xcode will not be happy, letting you know that <code>Command CompileSwift failed with a nonzero exit code</code>. Not exactly the most helpful, though digging in to the build output yields this gem:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="err">$</span><span class="p">@</span><span class="n">_opaqueReturnTypeOf</span><span class="p">(</span><span class="s">&#34;$s7SwiftUI5ScenePAAE14backgroundTask_6actionQrAA010BackgroundE0Vyqd__qd_0_G_qd_0_qd__YaYbcts8SendableRd__sAIRd_0_r0_lF&#34;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="n">__</span><span class="p">&lt;</span><span class="n">WindowGroup</span><span class="p">&lt;</span><span class="n">ModifiedContent</span><span class="p">&lt;</span><span class="n">SimpleWeatherView</span><span class="p">,</span> <span class="n">_EnvironmentKeyWritingModifier</span><span class="p">&lt;</span><span class="nb">Optional</span><span class="p">&lt;</span><span class="n">WeatherDataStore</span><span class="p">&gt;</span><span class="o">&gt;&gt;&gt;</span><span class="p">,</span> <span class="p">(),</span> <span class="p">()</span><span class="o">&gt;</span> <span class="c1">// user: %38</span>
</span></span><span class="line"><span class="cl"><span class="n">Please</span> <span class="n">submit</span> <span class="n">a</span> <span class="n">bug</span> <span class="n">report</span> <span class="p">(</span><span class="n">https</span><span class="p">:</span><span class="c1">//swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.</span>
</span></span></code></pre></div><p>Turns out that for now you will need to encapsulate the view and its modifiers into a single view like this:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-swift" data-lang="swift"><span class="line"><span class="cl"><span class="p">@</span><span class="n">main</span>
</span></span><span class="line"><span class="cl"><span class="kd">struct</span> <span class="nc">WeatherApp</span><span class="p">:</span> <span class="n">App</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kd">var</span> <span class="nv">body</span><span class="p">:</span> <span class="n">some</span> <span class="n">Scene</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">WindowGroup</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">	    <span class="n">TemporaryWrapperView</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="p">.</span><span class="n">backgroundTask</span><span class="p">(.</span><span class="n">appRefresh</span><span class="p">(</span><span class="s">&#34;WeatherCheck&#34;</span><span class="p">))</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">await</span> <span class="n">updateWeatherData</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kd">struct</span> <span class="nc">TemporaryWrapperView</span><span class="p">:</span> <span class="n">View</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="p">@</span><span class="n">StateObject</span> <span class="kd">var</span> <span class="nv">weatherData</span> <span class="p">=</span> <span class="n">WeatherDataStore</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">    
</span></span><span class="line"><span class="cl">    <span class="kd">var</span> <span class="nv">body</span><span class="p">:</span> <span class="n">some</span> <span class="n">View</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">SimpleWeatherView</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">            <span class="p">.</span><span class="n">environmentObject</span><span class="p">(</span><span class="n">weatherData</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>For more details check out <a href="https://developer.apple.com/forums/thread/710123">this thread</a> on the Apple Developer forums. As stated there, if all is well this workaround should hopefully not be needed once Xcode 14.1 ships.</p>
]]></content><id>https://cubefreecoder.com/posts/background-task-compiler-error/</id><author><name>David Bireta</name></author></entry><entry><title>Feedback most welcome</title><link rel="alternate" type="text/html" href="https://cubefreecoder.com/posts/feedback-most-welcome/"/><published>2020-08-23T00:00:00Z</published><updated>2020-08-23T00:00:00Z</updated><summary>&lt;p&gt;I woke up this morning to find out that I had gotten a response to a Feedback Assistant bug I had filed for a recent beta version of Xcode. The issue was around the re-ordering of the new document tabs. This really made my day that someone took the time to fix this. It was such a small issue, but was annoying enough that I actually submitted a Feedback Assistant, something I haven&amp;rsquo;t done too often. I know not every Feedback is able to be fixed, but it is always reassuring to know they are being looked at and addressed when possible. In fact, this has put me in such a good mood that I&amp;rsquo;ve already filed two more Feedbacks this morning :]&lt;/p&gt;</summary><content type="html"><![CDATA[<p>I woke up this morning to find out that I had gotten a response to a Feedback Assistant bug I had filed for a recent beta version of Xcode. The issue was around the re-ordering of the new document tabs. This really made my day that someone took the time to fix this. It was such a small issue, but was annoying enough that I actually submitted a Feedback Assistant, something I haven&rsquo;t done too often. I know not every Feedback is able to be fixed, but it is always reassuring to know they are being looked at and addressed when possible. In fact, this has put me in such a good mood that I&rsquo;ve already filed two more Feedbacks this morning :]</p>
<blockquote>
<p>One quick aside - when my Feedback was responded to it was <em>removed</em> from <code>Submitted</code> and moved to <code>Inbox</code>. I didn&rsquo;t realize this had happened as the issue got mixed in with all the other beta release note messages in the Inbox. I actually thought it had been deleted. Once I responded on the issue, it moved back to the <code>Sumbmitted</code> section. I guess this kind of makes sense, but something to keep in mind.</p>
</blockquote>
]]></content><id>https://cubefreecoder.com/posts/feedback-most-welcome/</id><author><name>David Bireta</name></author></entry><entry><title>What's new in Xcode 12 ... ?</title><link rel="alternate" type="text/html" href="https://cubefreecoder.com/posts/whats-new-in-xcode-12/"/><published>2020-06-30T00:00:00Z</published><updated>2020-06-30T00:00:00Z</updated><summary>&lt;p&gt;One of my favorite sessions to watch from WWDC each year is the &lt;code&gt;What's new with Xcode&lt;/code&gt;. As I quickly scanned the videos from this year I realized it was missing. What gives?&lt;/p&gt;
&lt;p&gt;As I&amp;rsquo;ve been making my way through the videos though, I have realized that it &lt;em&gt;is&lt;/em&gt; there, just in a much more subtle way.&lt;/p&gt;
&lt;p&gt;The pre-recorded session videos allow for a much tighter experience. Not only can more content be shared in the same amount of time, but &lt;em&gt;how&lt;/em&gt; it is shared can be altered. I have only seen a couple of instances so far where a code snippet was used to drop in a huge piece of code (I&amp;rsquo;m looking at you Core ML). Instead, most of the presenters actually typed, in &amp;ldquo;real time&amp;rdquo;, the code they were demonstrating. I think this is a huge improvement to the sessions of previous years where the new features were given as a bulleted list. I loved learning about the new features and tools in Xcode simply by watching someone use them in a normal context. Having these distributed out among so many of the videos also feels like they were able to share more of the changes that were made.&lt;/p&gt;</summary><content type="html"><![CDATA[<p>One of my favorite sessions to watch from WWDC each year is the <code>What's new with Xcode</code>. As I quickly scanned the videos from this year I realized it was missing. What gives?</p>
<p>As I&rsquo;ve been making my way through the videos though, I have realized that it <em>is</em> there, just in a much more subtle way.</p>
<p>The pre-recorded session videos allow for a much tighter experience. Not only can more content be shared in the same amount of time, but <em>how</em> it is shared can be altered. I have only seen a couple of instances so far where a code snippet was used to drop in a huge piece of code (I&rsquo;m looking at you Core ML). Instead, most of the presenters actually typed, in &ldquo;real time&rdquo;, the code they were demonstrating. I think this is a huge improvement to the sessions of previous years where the new features were given as a bulleted list. I loved learning about the new features and tools in Xcode simply by watching someone use them in a normal context. Having these distributed out among so many of the videos also feels like they were able to share more of the changes that were made.</p>
<p>While it&rsquo;s clear that my favorite video was missing from WWDC this year, it is just as clear that it doesn&rsquo;t need to be there. The general consensus seems to be that the sessions were well received this year, and I have to agree. I really hope that this is something we can continue to expect from WWDC in the years to come.</p>
]]></content><id>https://cubefreecoder.com/posts/whats-new-in-xcode-12/</id><author><name>David Bireta</name></author></entry></feed>