<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Swift on Robert Harrison</title>
    <link>https://robertharrison.ca/tags/swift/</link>
    <description>Recent content in Swift on Robert Harrison</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Fri, 18 Jul 2025 18:00:00 -0700</lastBuildDate>
    <atom:link href="https://robertharrison.ca/tags/swift/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Swift tip: Properly deprecating code</title>
      <link>https://robertharrison.ca/blog/swift-deprecating-code/</link>
      <pubDate>Fri, 18 Jul 2025 18:00:00 -0700</pubDate>
      <guid>https://robertharrison.ca/blog/swift-deprecating-code/</guid>
      <description>&lt;p&gt;As codebases evolve, we inevitably accumulate functions, classes, and APIs that need to be removed or replaced. Use &lt;code&gt;@available(*, deprecated, message: &amp;quot;...&amp;quot;)&lt;/code&gt; to mark functions as deprecated in your Swift projects:&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;@available(*, deprecated, message: &amp;#34;This function will be removed in a future release.&amp;#34;)&#xA;func oldFunction() {&#xA;    // legacy code&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This gives developers, including your future self, a clear heads-up about upcoming changes while keeping code functional during the transition period. The compiler will show warnings but won&amp;rsquo;t break existing builds.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Swift Singleton with @unchecked Sendable</title>
      <link>https://robertharrison.ca/blog/swift-singleton-with-unchecked-sendable/</link>
      <pubDate>Sat, 01 Mar 2025 14:35:00 -0700</pubDate>
      <guid>https://robertharrison.ca/blog/swift-singleton-with-unchecked-sendable/</guid>
      <description>&lt;p&gt;A &lt;strong&gt;singleton&lt;/strong&gt; is a design pattern that ensures there is only one instance of a class. While convenient, there are drawbacks to using singletons, which are well documented elsewhere. Swift 6 provides some approaches to replacing Singletons, such as Actor. Sometimes, you run into a situation, such as re-using legacy code, where using a Singleton is preferable over refactoring to modern Swift techniques.&lt;/p&gt;&#xA;&lt;p&gt;Pre-Swift 6, a singleton could be written like so:&lt;/p&gt;</description>
    </item>
    <item>
      <title>Organize Code with Local Swift Packages</title>
      <link>https://robertharrison.ca/blog/organize-code-local-swift-packages/</link>
      <pubDate>Wed, 25 Sep 2024 09:17:00 -0700</pubDate>
      <guid>https://robertharrison.ca/blog/organize-code-local-swift-packages/</guid>
      <description>&lt;p&gt;There are several benefits to organizing your code into modules:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Readability&lt;/li&gt;&#xA;&lt;li&gt;Testability&lt;/li&gt;&#xA;&lt;li&gt;Reusability&lt;/li&gt;&#xA;&lt;li&gt;Maintainability&lt;/li&gt;&#xA;&lt;li&gt;And more &amp;hellip;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;By using an Xcode Workspace and Swift Packages, a monolithic project can be split up and organized, into smaller modules or packages. Utilities, for example, can go in one package, and the Data Layer can go in another package.&lt;/p&gt;&#xA;&lt;p&gt;Swift Packages can be local or remote. If the module can be used by other projects or teams, then consider using a &lt;strong&gt;remote&lt;/strong&gt; package. In which case, the remote package will require its own git repository. If the module is only usable by the main application, then use a &lt;strong&gt;local&lt;/strong&gt; package. The local package will live within the git repository of the main application. In this article, we’re going to use a &lt;strong&gt;local&lt;/strong&gt; package.&lt;/p&gt;</description>
    </item>
    <item>
      <title>Manage Multiple Projects with an Xcode Workspace</title>
      <link>https://robertharrison.ca/blog/manage-multiple-projects-xcode-workspace/</link>
      <pubDate>Mon, 23 Sep 2024 15:38:00 -0700</pubDate>
      <guid>https://robertharrison.ca/blog/manage-multiple-projects-xcode-workspace/</guid>
      <description>&lt;p&gt;By default, Xcode projects use an &lt;code&gt;.xcodeproj&lt;/code&gt; file to organize and structure the project. The project file contains code, resources, settings and targets for building a product.&lt;/p&gt;&#xA;&lt;p&gt;What if your project needs to reference another project? For example, prior to building your main app, you must first compile and link a library. Use an &lt;strong&gt;Xcode Workspace&lt;/strong&gt; (&lt;code&gt;.xcworkspace&lt;/code&gt;) to manage the dependencies between multiple projects. Xcode can use the workspace to compile the library first, then link it to your app. If your project uses &lt;a href=&#34;https://cocoapods.org&#34;  class=&#34;external-link&#34; target=&#34;_blank&#34; rel=&#34;noopener&#34;&gt;Cocoapods&lt;/a&gt;, then it’s using a Workspace.&lt;/p&gt;</description>
    </item>
    <item>
      <title>SwiftUI Toggle</title>
      <link>https://robertharrison.ca/blog/swiftui-toggle/</link>
      <pubDate>Tue, 22 Feb 2022 11:53:00 -0800</pubDate>
      <guid>https://robertharrison.ca/blog/swiftui-toggle/</guid>
      <description>&lt;p&gt;This article describes how to use the SwiftUI &lt;a href=&#34;https://developer.apple.com/documentation/swiftui/toggle&#34;  class=&#34;external-link&#34; target=&#34;_blank&#34; rel=&#34;noopener&#34;&gt;Toggle&lt;/a&gt;. By the end of the article you will have learned how to embed Toggle in a child View and perform the Toggle&amp;rsquo;s action in a parent View.&lt;/p&gt;&#xA;&lt;p&gt;Let&amp;rsquo;s start with the basics. Here is a variation of the example in the official &lt;a href=&#34;https://developer.apple.com/documentation/swiftui/toggle&#34;  class=&#34;external-link&#34; target=&#34;_blank&#34; rel=&#34;noopener&#34;&gt;documentation&lt;/a&gt;:&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;@State private var faceIdEnabled = false&#xA;var body: some View {&#xA;    Toggle(isOn: $faceIdEnabled) {&#xA;        Text(&amp;#34;Face ID&amp;#34;)&#xA;    }&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The above code will show a toggle control with &amp;ldquo;Face ID&amp;rdquo; for the title. By using a two-way binding on &lt;strong&gt;faceIdEnabled&lt;/strong&gt;, we control if the toggle is on or off.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
