<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>SwiftUI on Robert Harrison</title>
    <link>https://robertharrison.ca/tags/swiftui/</link>
    <description>Recent content in SwiftUI on Robert Harrison</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Fri, 22 Nov 2024 13:03:00 -0700</lastBuildDate>
    <atom:link href="https://robertharrison.ca/tags/swiftui/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Performance of SwiftUI Views with Conditional Branching</title>
      <link>https://robertharrison.ca/blog/swiftui-conditional-branching-performance/</link>
      <pubDate>Fri, 22 Nov 2024 13:03:00 -0700</pubDate>
      <guid>https://robertharrison.ca/blog/swiftui-conditional-branching-performance/</guid>
      <description>&lt;p&gt;Improve the performance of your SwiftUI views by replacing &lt;strong&gt;if-else&lt;/strong&gt; and &lt;strong&gt;switch&lt;/strong&gt; statements with the &lt;strong&gt;ternary&lt;/strong&gt; operator. &lt;/p&gt;&#xA;&lt;p&gt;To illustrate the issue, let’s start with the following code that uses an if-else statement to set the foreground color of an image:&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;struct ContentView: View {&#xA;    @State private var isFavorite = true&#xA;&#xA;    var body: some View {&#xA;        if isFavorite {&#xA;            Image(systemName: &amp;#34;star.fill&amp;#34;)&#xA;                .foregroundStyle(Color.yellow)&#xA;        } else {&#xA;            Image(systemName: &amp;#34;star.fill&amp;#34;)&#xA;                .foregroundStyle(Color.black)&#xA;        }&#xA;    }&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The above code has two issues. First, there is some duplicate code. Second, toggling &lt;strong&gt;isFavorite&lt;/strong&gt; between true and false causes SwiftUI to tear down and recreate the entire view. SwiftUI treats the if-else paths as separate Views and will do extra work to process the if and else paths. The same performance issue occurs with switch statements.&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>
