<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem:  Nafeez Ahmed</title>
    <description>The latest articles on Forem by  Nafeez Ahmed (@nafeezahmed).</description>
    <link>https://forem.com/nafeezahmed</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F966050%2F5a193950-d6e9-4d79-bd12-5480f3ee203c.JPG</url>
      <title>Forem:  Nafeez Ahmed</title>
      <link>https://forem.com/nafeezahmed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nafeezahmed"/>
    <language>en</language>
    <item>
      <title>What's new in SwiftUI Colors and SF Symbols? ft. WWDC'22</title>
      <dc:creator> Nafeez Ahmed</dc:creator>
      <pubDate>Fri, 06 Jan 2023 16:43:00 +0000</pubDate>
      <link>https://forem.com/nafeezahmed/whats-new-in-swiftui-colors-and-sf-symbols-ft-wwdc22-1a2f</link>
      <guid>https://forem.com/nafeezahmed/whats-new-in-swiftui-colors-and-sf-symbols-ft-wwdc22-1a2f</guid>
      <description>&lt;p&gt;SwiftUI was launched in 2019 and since then Apple has been constantly adding features and updates to make it production ready. Following that, there were drastic amount of updates made in SwiftUI 4. So, without further ado, let's dive into new updates on colors, styles and SF symbol implementation.​&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 You can download the new SF Symbols from &lt;a href="https://developer.apple.com/sf-symbols/"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Gradient Colors
&lt;/h1&gt;

&lt;p&gt;Before diving into gradient colors, let's get some context on foreground and background styles. So, foreground style (.foregroundStyle()) and background style (.backgroundStyle()) helps you add images, colors and gradients as the background for a particular view.&lt;/p&gt;

&lt;p&gt;Previously, for making background and foreground styles of monochromatic gradient, you could have to generate gradient views and use that as background. Now with SwiftUI 4, you could simply add .gradient to the color.&lt;/p&gt;

&lt;p&gt;Let's consider a shape and add a gradient color as foreground style.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RoundedRectangle(cornerRadius: 24)
            .foregroundStyle(Color.red.gradient)
            .padding()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KsoyAgkF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ubh97rdkr2fkimn7xekw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KsoyAgkF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ubh97rdkr2fkimn7xekw.jpg" alt="Image description" width="780" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's give another example. We will take some text in a vertical stack and add a gradient background.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GroupBox {
            VStack(spacing: 8) {
                Text("Hello, World!") ]
                    .bold()
                    .font(.system(.body, design: .rounded)) // 1 
                Text("Welcome to Swift Anytime")  
            }
        }
        .padding() // 2 
        .backgroundStyle(Color.orange.gradient) // 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EZBURcJB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxgll5n4f1pzhh3f96ji.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EZBURcJB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxgll5n4f1pzhh3f96ji.jpg" alt="Image description" width="780" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code Explanation:&lt;/p&gt;

&lt;p&gt;​1. For stylizing purposes, this special font has been added.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Added adding for spacing purposes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here is when the magic happens. The background style is being set to an orange gradient.​&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Wanna add a cool shadow to the gradient? Just add &lt;code&gt;.shadow()&lt;/code&gt; to the gradient and pick a type of gradient. In the following example, inner shadow is picked with certain radius.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  .backgroundStyle(
        Color.orange.gradient
            .shadow(.inner(radius: 8))
    )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6G59Ja4n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tpg6thgeomzrcdn7azzd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6G59Ja4n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tpg6thgeomzrcdn7azzd.jpg" alt="Image description" width="780" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  SF Symbols
&lt;/h1&gt;

&lt;p&gt;Apple has expanded the library of the SF Symbols to 700+ symbols. Along with that, new customization and configuration regarding rendering mode and variable colors have been there.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 New symbols include representations of home objects, fitness, health, currencies, and many more.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Source : &lt;a href="https://developer.apple.com/sf-symbols/"&gt;Apple&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Category Edition
&lt;/h1&gt;

&lt;p&gt;5 new categories have been added to SF Symbols:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Camera &amp;amp; Photos&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Accessibility&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Privacy &amp;amp; Security&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Home&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fitness&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wayuN39s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4akpqp2pqb72j4x8m0ic.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wayuN39s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4akpqp2pqb72j4x8m0ic.png" alt="Image description" width="786" height="690"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Variable Color
&lt;/h1&gt;

&lt;p&gt;Another innovative thing they added to SF Symbols is variable color. Meaning you can supply a Double value (usually 0-1) and it changes the state of the symbol based on the variable value. Keep in mind only certain SF Symbols are available for variable color. You can find it in "Variable Color" section on SF Symbol left pane. Also for being on the safe side, the lower bound of the variable color is considered for the decimals.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Image(systemName: "rays", variableValue: variable) // 1
            .font(.largeTitle)
            .onTapGesture {
                variable += 0.2 // 2
            }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VigYZXrf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ecam5ijvjqifq1png1bz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VigYZXrf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ecam5ijvjqifq1png1bz.gif" alt="Image description" width="636" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code Explanation:&lt;/p&gt;

&lt;p&gt;​1. Here, the image is being initialized with SF symbols with a pre-defined variable value.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For convenience in understanding, a tap gesture example has been used. On the tap gesture, the variable value is increased by 0.2 for sake of simplicity.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As you can see, on tap gesture, it shows different range of that particular SF symbols.&lt;/p&gt;

&lt;h1&gt;
  
  
  Rendering Mode
&lt;/h1&gt;

&lt;p&gt;With SwiftUI 4, Apple has introduced 4 rendering modes for SF Symbols: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Monochrome&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hierarchial&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Palette&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Multicolor&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rendering mode can be using .symbolRenderingMode(...) style modifier.&lt;/p&gt;

&lt;h1&gt;
  
  
  Monochrome Mode
&lt;/h1&gt;

&lt;p&gt;Monochrome SF symbols, as the name explains, are meant to have a single color.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nQE_7Imb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uuqf6f24awwzjll084qq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nQE_7Imb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uuqf6f24awwzjll084qq.jpg" alt="Image description" width="780" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Hierarchical Mode
&lt;/h1&gt;

&lt;p&gt;Hierarchical mode has multiple layers which are rendered with different opacities for the given foreground style.  You can also add a particular color to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Image(systemName: "calendar.badge.plus")
    .symbolRenderingMode(.hierarchical)
    .font(.system(size: 200))
    .foregroundStyle(.blue)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Multicolor Mode
&lt;/h1&gt;

&lt;p&gt;Multicolor mode has multiple layers where different colors can be embedded in the iconography by default.&lt;/p&gt;

&lt;p&gt;Result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ARLm1UHA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/duspuprl9ye3hj7wmjke.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ARLm1UHA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/duspuprl9ye3hj7wmjke.jpg" alt="Image description" width="780" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Palette Mode
&lt;/h1&gt;

&lt;p&gt;Palette mode is just like a more customizable version of the multi-colored mode. It allows you to pick the foreground colors/style. Cool twist, you can apply the same gradients you learnt in earlier sections.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Image(systemName: "iphone")
    .symbolRenderingMode(.palette)
    .font(.system(size: 200))
    .foregroundStyle(Color.teal.gradient, Color.blue.gradient) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R0GIe7TE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfekn8mhbjtgpl7oez6a.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R0GIe7TE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfekn8mhbjtgpl7oez6a.jpg" alt="Image description" width="780" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wrapping up the article, you learned about the new SF Symbols updates -  rendering modes, variable color and new category edition - color gradients, foreground/background style and more.&lt;/p&gt;

&lt;p&gt;We at Swift Anytime have the mission to make learn iOS development the way everyone enjoys it. You can check out our articles on &lt;a href="https://www.swiftanytime.com/tag/swiftui/"&gt;SwiftUI&lt;/a&gt;, &lt;a href="https://www.swiftanytime.com/tag/swift/"&gt;Swift&lt;/a&gt;, &lt;a href="https://www.swiftanytime.com/tag/interview/"&gt;iOS Interview Questions&lt;/a&gt; and get started with your iOS journey today.​​&lt;/p&gt;

&lt;p&gt;Eat. Sleep. Swift Anytime. Repeat.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to use Geometry Reader in SwiftUI</title>
      <dc:creator> Nafeez Ahmed</dc:creator>
      <pubDate>Thu, 05 Jan 2023 15:49:41 +0000</pubDate>
      <link>https://forem.com/nafeezahmed/how-to-use-geometry-reader-in-swiftui-4ajf</link>
      <guid>https://forem.com/nafeezahmed/how-to-use-geometry-reader-in-swiftui-4ajf</guid>
      <description>&lt;p&gt;SwiftUI Layout works very differently than UIKit; Wherein UIKit we dealt with raw frames, and either added constraints accordingly or have some concrete frame-set to specify the position of views, in SwiftUI, the layouts are defined in form of stacks, spacers and paddings.&lt;/p&gt;

&lt;p&gt;This layout system provides us a lot of simple and flexible ways to define our view's position, but sometimes scenarios arise where you need to have specific frames or size set for a view either by setting some concrete value for which SwiftUI have provided .frame modifier. There are also conditions where you need to define the size or position of a view relative to its parent, and for this particular use case, SwiftUI have provided us with GeometryReader.&lt;/p&gt;

&lt;p&gt;GeometryReader is an special container view like any other container view such as VStack, ZStack and Group but the special part is, the closure this container has, have an parameter of type GeometryProxy with contains information about this containers size and safeAreaInsets, also it has an function frame(in: CoordinateSpace) -&amp;gt; CGRect which give us frames for an certain CoordinateSpace (more on this in later section of the article).&lt;/p&gt;

&lt;p&gt;You can use this information to size/position our view relative to the size/position of it's superview and any view in the hierarchy.&lt;/p&gt;

&lt;p&gt;Before you indulging deeper into geometry reader, let's look at an alternative way to fetch frame properties of a view without using geometry readers.&lt;/p&gt;

&lt;h1&gt;
  
  
  An Alternative
&lt;/h1&gt;

&lt;p&gt;So, what if you want an element to consume like 1/4th or 1/3rd of the screen? Well, you can go UIScreen.main.bounds. The downside is it only works if your app is only portrait since it doesn't adapt to the different orientations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;VStack(spacing: 0) {
            Color.orange
                .frame(height: UIScreen.main.bounds.height * (1/3)) // 1
            Color.red
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Result:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzsf2o9bz80yfqfyhiddb.JPEG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzsf2o9bz80yfqfyhiddb.JPEG" alt="Image description" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code Explanation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Here, the height of the orange color is set to the 1/3rd of the screen height. You can see that the screen height is fetched from UIScreen.main.bounds&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Basic Implementation
&lt;/h1&gt;

&lt;p&gt;GeometryReader works by wrapping our view inside the view builder closer, it acts as a container.&lt;/p&gt;

&lt;p&gt;For accessing the frame properties, you could just wrap the view around a geometry reader and use the geometry proxy in it. The job of geometry proxy is to provide you with the frame in a certain coordinate space (talked in detail in upcoming sections).&lt;/p&gt;

&lt;p&gt;Let’s see geometry reader in action by having a couple of colors in a VStack:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; GeometryReader { reader in // 1 
            VStack {

                    Color.blue
                        .frame(height: reader.size.height * (1/5)) // 2
                        .cornerRadius(10)
                        .padding()

                    Color.green
                        .frame(height: reader.size.height * (1/2)) // 3
                        .cornerRadius(10)
                        .padding()

            }
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Result:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgus5n26kgnvrfg2fu77z.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgus5n26kgnvrfg2fu77z.gif" alt="Image description" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code Explanation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;As we discussed earlier, here the view is wrapped around the reader.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here, color blue was used in VStack (for sake of simplicity) with height that is 1/5th of the whole screen.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here, green color is made to cover half of the screen.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The key here is to observe that even if the simulator changes the orientation, the view looks as excepted and adapts to it (which would not possible with UIScren.main.bounds).&lt;/p&gt;

&lt;h1&gt;
  
  
  Coordinate Spaces
&lt;/h1&gt;

&lt;p&gt;There are three coordinate spaces available to use in geometry reader:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Global - Frame relative to the whole screen/superview ie. the whole content view&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Local - Frame relative to the immediate parent view&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Custom - The custom coordinate space (identifying a view by adding .coordinateSpace(name: ""))&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Global and Local Coordinate Space
&lt;/h1&gt;

&lt;p&gt;Let's make a list item with text wrapped around the geometry reader. The text should display the mid x and y value of the global and local frames. Let's also add some bold title just in a VStack in order to add some extra content to the overall view.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; VStack(alignment: .leading) {
            Text("Title")
                .bold()
                .font(.title)
                .padding()
            List {
                GeometryReader { reader in
                    Text("Global : (\(Int(reader.frame(in: .global).midX)), \(Int(reader.frame(in: .global).midY))) Local: (\(Int(reader.frame(in: .local).midX)), \(Int(reader.frame(in: .local).midY)))")
                }
            }
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Result:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmmzj7d0szsmfmejytddp.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmmzj7d0szsmfmejytddp.gif" alt="Image description" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can observe how the global Y coordinate changes as you change the scroll position of the item. Moreover, the local coordinates remain the same since that frame is relative to the list cell view.&lt;/p&gt;

&lt;h1&gt;
  
  
  Custom Coordinate Space
&lt;/h1&gt;

&lt;p&gt;Now that you understood global and local coordinate space, let's take a look at custom coordinate space.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var body: some View {

        GeometryReader { reader in
            ZStack { // 1 
                Color.blue
                    .coordinateSpace(name: "blue") // 2 
                    .cornerRadius(10)

                Color.red
                    .frame(height: reader.frame(in: .named("blue")).height * 1/2) // 3 
                    .cornerRadius(10)
                    .padding()

            }.padding()
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Result:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2zv8dkoexjsjs0u1hu3f.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2zv8dkoexjsjs0u1hu3f.jpeg" alt="Image description" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Code Explanation:
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Here, there is a ZStack in which are we are going to put the content&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The blue view is given the coordinate space name of "blue" so this can be used later&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The height of the red view is calculated such that it is half of the coordinate space "blue" (aka the blue view)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here, the key is .coordinateSpace(name: "blue"). If you wouldn't have named that coordinated space, the red view wouldn't have been able to get blue view's frame.&lt;/p&gt;

&lt;p&gt;Moreover, you can see how using a coordinate space can give use frame of the specific view required.&lt;/p&gt;

&lt;p&gt;Congratulations! You deserve to celebrate this moment. Today you learned about geometry reader in SwiftUI and what is its significance, alternative way to get the frame properties using UIScreen bounds and different type of coordinate spaces and its usage. We encourage you to read more related articles like &lt;a href="https://www.swiftanytime.com/colors-and-gradient-in-swiftui/" rel="noopener noreferrer"&gt;Colors and Gradient in SwiftUI&lt;/a&gt; till then, have a great time ahead.&lt;/p&gt;

&lt;p&gt;Eat. Sleep. Swift Anytime. Repeat.&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>productivity</category>
      <category>vscode</category>
    </item>
    <item>
      <title>How to implement Apple's SharePlay? WWDC'22</title>
      <dc:creator> Nafeez Ahmed</dc:creator>
      <pubDate>Wed, 04 Jan 2023 13:48:13 +0000</pubDate>
      <link>https://forem.com/nafeezahmed/how-to-implement-apples-shareplay-wwdc22-4k8m</link>
      <guid>https://forem.com/nafeezahmed/how-to-implement-apples-shareplay-wwdc22-4k8m</guid>
      <description>&lt;p&gt;With the launch of iOS 15 (in June 2021), Apple launched a new feature which allows you to experience content together with other people on a FaceTime call.&lt;/p&gt;

&lt;p&gt;Integrating SharePlay to your applications is really straightforward once you understand how the objects work and when the methods should be triggered. So, without further ado, let's dive straight into the fundamentals.&lt;/p&gt;

&lt;h1&gt;
  
  
  Prerequisites
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Developer account&lt;/li&gt;
&lt;li&gt;Two devices to test it on&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: If not possible, you can implement catalyst in your app and test SharePlay with your computer and your personal device.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Testing
&lt;/h1&gt;

&lt;p&gt;As it is a FaceTime feature, it cannot be tested on simulator. You need at least two devices. Or as mentioned above, you can implement catalyst (or even make a mac app) and test the SharePlay.&lt;/p&gt;

&lt;h1&gt;
  
  
  How testing goes
&lt;/h1&gt;

&lt;p&gt;You run the app on both of your devices. Then you call one device from FaceTime or maybe share a common FaceTime link (and reuse it throughout the testing).&lt;/p&gt;

&lt;h1&gt;
  
  
  Setting up SharePlay and adding entitlements
&lt;/h1&gt;

&lt;p&gt;For integrating SharePlay, one simply needs to add "Group Activities" in Your Project -&amp;gt; Signing &amp;amp; Capabilities -&amp;gt; "+ Capability"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Gk7u9PRR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qkfep9jj8va8mydrkexa.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Gk7u9PRR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qkfep9jj8va8mydrkexa.gif" alt="Image description" width="714" height="722"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will add an entitlements file to your app.&lt;/p&gt;

&lt;h1&gt;
  
  
  Coordination Manager
&lt;/h1&gt;

&lt;p&gt;One is primarily going to use this for prepare your sessions to play and for coordination your AVPlayer.&lt;/p&gt;

&lt;p&gt;Feel free to use the boiler plate code given the apple in their &lt;a href="https://developer.apple.com/documentation/avfoundation/media_playback/supporting_coordinated_media_playback"&gt;demo project&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Following apple's coordination manager code, we have prepareToPlay() which prepares for activation and according to the user's response, takes further steps.&lt;/p&gt;

&lt;p&gt;The prepareForActivation within prepareToPlay will prompt the user for something similar to the following alert:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--STxxUIAk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5wsnihb6vpv79d1y6vb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--STxxUIAk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5wsnihb6vpv79d1y6vb.png" alt="Image description" width="780" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you select an option, FaceTime remembers your preference and plays according to that.&lt;/p&gt;

&lt;h1&gt;
  
  
  GroupActivity
&lt;/h1&gt;

&lt;p&gt;This is the object you are going to use throughout the app to get the sessions and url.&lt;/p&gt;

&lt;p&gt;The following is an example of GroupActivity object for video playing sharply object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import GroupActivities
import AVKit


@available(iOS 15, *)
struct VideoGroupActivity: GroupActivity {

    let video: Video 

    static var activityIdentifier = "com.example.SharePlayApp.SharePlayApp"


    var metadata: GroupActivityMetadata {
        var metadata = GroupActivityMetadata()
        metadata.type = .watchTogether
        metadata.fallbackURL = Bundle.main.url(forResource: video.url, withExtension: "mp4")
        metadata.title = video.videoTitle
        return metadata
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Metadata type: There are three types available. watchTogether (for video activities), listenTogether (for audio activities) and generic (for custom SharePlay experiences)&lt;/p&gt;

&lt;p&gt;Metadata title: This is the title that is going to appear on the FaceTime top bar once the SharePlay starts.&lt;/p&gt;

&lt;p&gt;Fallback URL: This is the url we are going to retrieve and play when a sharplay session is detected. This may not make complete sense right now but later when we make jump to a tutorial.&lt;/p&gt;

&lt;h1&gt;
  
  
  How SharePlay integration works:
&lt;/h1&gt;

&lt;p&gt;Once you add entitlements, add Coordinaton Manager and setup the GroupActivity, all it takes it to follow call some methods and the SharePlay will be working well!&lt;/p&gt;

&lt;p&gt;The following is how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;You fetch the session from GroupActivity.sessions() using task.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You prepareToPlay using CoordinationManager.prepareToPlay()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You join the session using session.join()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You coordinate the player using player.playbackCoordinator.coordinateWithSession(session)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can leave the session (ending session locally) using sessions.leave() or end the session (end it completely for everyone) using session.end().&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Yep, as easy as that!&lt;/p&gt;

&lt;p&gt;Concluding the article, today you learned about different components of SharePlay, integrating it in your Xcode project, and learned end-to-end SharePlay implementation.&lt;/p&gt;

&lt;h1&gt;
  
  
  Where to go from here?
&lt;/h1&gt;

&lt;p&gt;With WWDC 2022, Apple launched lot of new features for SharePlay Collaboration. Feel free to go through WWDC Sessions. The following can be your start point: &lt;a href="https://www.swiftanytime.com/how-to-implement-apples-shareplay-wwdc22/"&gt;MeetGroup Activities - WWDC21&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Stay tuned to know more about SharePlay, test its demo app and make yours. Consider subscribing to &lt;a href="https://www.swiftanytime.com/signup/"&gt;our newsletter&lt;/a&gt; to get the latest updates directly to your inbox.&lt;/p&gt;

&lt;p&gt;Eat. Sleep. Swift Anytime. Repeat.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Visual Effects Blur in SwiftUI</title>
      <dc:creator> Nafeez Ahmed</dc:creator>
      <pubDate>Mon, 02 Jan 2023 10:04:01 +0000</pubDate>
      <link>https://forem.com/nafeezahmed/visual-effects-blur-in-swiftui-4fm9</link>
      <guid>https://forem.com/nafeezahmed/visual-effects-blur-in-swiftui-4fm9</guid>
      <description>&lt;p&gt;Swift provides with different ways of displaying visual effects like blur and vibrancy effects. These can mostly be used as background for views and can be used to improve the overall app design. You can observe these blur effects throughout iOS like in app library, control center and app dock.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75de5gyeqyj4iawdm06t.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F75de5gyeqyj4iawdm06t.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Materials
&lt;/h1&gt;

&lt;p&gt;Adding visual effect as background in iOS 15 is as easy as doing the following:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

 Text("Hello, World!")
            .frame(width: 200, height: 50)
            .background(.thickMaterial)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Note:
&lt;/h3&gt;

&lt;p&gt;If you are using any frame in your view, use the .background after that so that the background effect is applied for the required frame size.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyle3htufy5noiluzjl3x.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyle3htufy5noiluzjl3x.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This visual effect is actually an enum called Material which has thin, thick and regular properties.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffzdl7ur8d3r2qjsepthb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffzdl7ur8d3r2qjsepthb.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Visual Effect using UIViewRepresentable
&lt;/h1&gt;

&lt;p&gt;Above was the case for iOS 15+ only. But for &amp;gt;iOS15, you can use a mini UIViewRepresentable to implement visual effect into your app.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

struct VisualEffect: UIViewRepresentable {
    @State var style : UIBlurEffect.Style // 1
    func makeUIView(context: Context) -&amp;gt; UIVisualEffectView {
        return UIVisualEffectView(effect: UIBlurEffect(style: style)) // 2
    }
    func updateUIView(_ uiView: UIVisualEffectView, context: Context) {
    } // 3 
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Code Explanation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Defining the state variable to take in the blur effect enum as a parameter.&lt;/li&gt;
&lt;li&gt;Returning a UIVisualEffectView (UIKit element) with the style variable.&lt;/li&gt;
&lt;li&gt;Since no view updates are required, no code is added to updateUIView.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Usage:
&lt;/h1&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Text("Hello, World!”)
            .frame(width: 150, height: 50, alignment: .center)
            .background(VisualEffect(style: .systemThickMaterial))


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faw8m6noioj3r27xt8xpb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faw8m6noioj3r27xt8xpb.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There can be cases where on adding the blur effect to the view, the visual effect can look no different than a solid background color. In that case, you just need to play around with the alpha and opacity value of the visual effect.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Text("Hello, World!")
    .frame(width: 150, height: 50, alignment: .center)
    .background(.thin)
    .background(
        VisualEffect(style: .systemThickMaterial)
            .opacity(0.8)
    )


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you are looking forward to implement vibrancy effects like before, SwiftUI doesn't support the vibrancy effect natively. For that, you could go for &lt;a href="https://github.com/lucasbrown/swiftui-visual-effects" rel="noopener noreferrer"&gt;Swift Packages like SwiftUI Visual Effects by Lucas Brown.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F35aa2r7jt4l3kktjefol.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F35aa2r7jt4l3kktjefol.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Wrapping up the article, today you learned about implementatoin of visuals effects in SwiftUI by using Material and about integrating the UIKit version. We encourage you to go over the concept of &lt;a href="https://www.swiftanytime.com/colors-and-gradient-in-swiftui/" rel="noopener noreferrer"&gt;Colors &amp;amp; gradient in SwiftUI&lt;/a&gt; . Till then,&lt;/p&gt;

&lt;p&gt;Eat. Sleep. Swift Anytime. Repeat.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Ultimate guide on Timer in Swift</title>
      <dc:creator> Nafeez Ahmed</dc:creator>
      <pubDate>Tue, 27 Dec 2022 15:20:49 +0000</pubDate>
      <link>https://forem.com/nafeezahmed/ultimate-guide-on-timer-in-swift-3icn</link>
      <guid>https://forem.com/nafeezahmed/ultimate-guide-on-timer-in-swift-3icn</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgi8cbt1d65gm05t55zkc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgi8cbt1d65gm05t55zkc.png" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
A timer is a class used to perform any task after a specific time interval. Timers are super handy in Swift for scheduling tasks with a delay or scheduling repeating work.&lt;br&gt;
In this article, we are going to cover the following things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to schedule tasks?&lt;/li&gt;
&lt;li&gt;Repeating and non-repeating timers&lt;/li&gt;
&lt;li&gt;Using RunLoop modes&lt;/li&gt;
&lt;li&gt;Keep track of timer&lt;/li&gt;
&lt;li&gt;Timer optimisation to reduce energy and power impact
class Timer : NSObject&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Why Timers are required?
&lt;/h1&gt;

&lt;p&gt;You often face requirements where you need to perform a particular task repeatedly within a particular time interval, or scenarios where you need to perform certain actions after a defined time. These are the ideal condition where you could leverage Timers in your codebase.&lt;/p&gt;
&lt;h2&gt;
  
  
  Timer can be used in some common use cases:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Say you have to automatically call a particular method every 5 seconds.&lt;/li&gt;
&lt;li&gt;You want to send a local notification once you terminate the app.&lt;/li&gt;
&lt;li&gt;Sync your cart details with the backend by sending updated details after each X time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In above scenarios we can use timers to achieve the repeating and scheduling behaviour.&lt;/p&gt;
&lt;h2&gt;
  
  
  Repeating Vs Non-repeating Timers
&lt;/h2&gt;

&lt;p&gt;You have to specify whether a timer is repeating or non-repeating at the time of creation. Here is the main difference between both:&lt;/p&gt;

&lt;p&gt;A non-repeating timer fires once and then invalidates itself automatically, so, it prevents the timer from firing again.&lt;/p&gt;

&lt;p&gt;A repeating timer fires and then reschedules itself on the same run loop. A repeating timer always schedules itself based on the scheduled firing time, as opposed to the actual firing time.&lt;/p&gt;

&lt;p&gt;For example, if a timer is scheduled to fire at a specific time and every 10 seconds after that, the scheduled firing time will always fall on the original 10-second time intervals, even if the actual firing time gets delayed.&lt;/p&gt;
&lt;h2&gt;
  
  
  Scheduling a repeating task
&lt;/h2&gt;

&lt;p&gt;Creating a repeating task is very simple.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;override func viewDidLoad() {
    super.viewDidLoad()

    // scheduling a timer in repeat mode after each 2 seconds.
    let timer = Timer.scheduledTimer(timeInterval: 2,
                                     target: self,
                                     selector: #selector(handleTimerExecution),
                                     userInfo: nil,
                                     repeats: true)
}

@objc private func handleTimerExecution() {
    print("timer executed...")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code,&lt;/p&gt;

&lt;p&gt;A timer is created with the class method scheduledTimer by passing parameters like time interval, userInfo and repeat mode, etc. Remember that time intervals always take time in seconds.&lt;/p&gt;

&lt;p&gt;Here, we set the time to 2 seconds, which means the action (handleTimerExecution) will execute after every 2 seconds.&lt;br&gt;
With userInfo you can pass the extra information along with the Timer object. The object should be in dictionary format.&lt;/p&gt;

&lt;p&gt;We have used the target-action mechanism to perform a task.&lt;br&gt;
Most importantly, you have set the repeat parameter true to make the timer repeatable.&lt;/p&gt;

&lt;p&gt;When you run the code above, you will see that the handleTimerExecution method will continue to be called indefinitely. Because we haven't handled the timer's ended case in the above code.&lt;/p&gt;

&lt;p&gt;Even though the timer constant was created inside the viewDidLoad() local context, it will outlive that function, since Runloop keeps a reference to the timer object.&lt;/p&gt;

&lt;p&gt;Note: The @objc attribute makes the handleTimerExecution() function available in Objective-C. And the timer class is a part of Objective-C runtime, that's why we need the @objc attribute here.&lt;/p&gt;

&lt;p&gt;In iOS, target-action is a mechanism to call a particular method on a certain object. Here target is something on which action method is going to call and action means a method which will be called.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to send info with timer?
&lt;/h2&gt;

&lt;p&gt;Really, can we send information along with a timer??? Yes, it's simple to pass some useful info while creating a timer object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private var timer: Timer?

override func viewDidLoad() {
    super.viewDidLoad()

    let userInfo = ["fullName": "John Martin", "age": 30] as [String : Any]

    self.timer = Timer.scheduledTimer(timeInterval: 1,
                                      target: self,
                                      selector: #selector(handleTimerExecution(_:)),
                                      userInfo: userInfo,
                                      repeats: true)
}

@objc private func handleTimerExecution(_ timer: Timer) {
    if let userInfo = timer.userInfo as? [String: Any] {
        print("user info: \(userInfo)")
        self.timer?.invalidate()
    } 

    // here is a fallback to end timer if userInfo not found
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Scheduling a repeating task using a closure:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let timer = Timer.scheduledTimer(withTimeInterval: 2.0,
                                         repeats: true) { timer in
    print("timer executed...")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above, we have scheduled a repeating timer using a closure. In this way, you will receive the timer's execution within the same block. You don't need to define a separate method like a target-action mechanism.&lt;/p&gt;

&lt;p&gt;Did you find this cool???? yesss, closure based timer is really cool.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to stop a repeating timer?
&lt;/h2&gt;

&lt;p&gt;To stop a repeated timer in Swift is very simple. A timer class does provide an instance method called invalidate().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private var timerCurrentCount = 0

let timer = Timer.scheduledTimer(withTimeInterval: 2.0,
                                         repeats: true) { timer in

    // check for max execution count of timer.
    if self.timerCurrentCount == 5 {
        timer.invalidate() // invalidate the timer
    } else {
        print("timer executed...")
        self.timerCurrentCount += 1
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Points to note here:
&lt;/h2&gt;

&lt;p&gt;You can have to take care of the timer's termination very carefully. Because a repeating timer will never end if the end conditions do not match.&lt;/p&gt;

&lt;p&gt;For the target-action mechanism, you have to make the timer variable an instance variable outside the function like in the example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private var timer: Timer?

override func viewDidLoad() {
    super.viewDidLoad()

    self.timer = Timer.scheduledTimer(timeInterval: 2,
                                      target: self,
                                      selector: #selector(handleTimerExecution),
                                      userInfo: nil,
                                      repeats: true)
}

@objc private func handleTimerExecution() {
    if self.timerCurrentCount == 5 {
        self.timer?.invalidate() // invalidating timer
    } else {
        print("timer executed...")
        self.timerCurrentCount += 1
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Scheduling a non-repeating task
&lt;/h2&gt;

&lt;p&gt;Using a timer, creating a non-repeating task is very similar to the repeated method. While you create a timer, you set a boolean to the repeat parameter.&lt;/p&gt;

&lt;p&gt;To make timer non-repeatable, simply sets this parameter as false.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;override func viewDidLoad() {
    super.viewDidLoad()

    let timer = Timer.scheduledTimer(timeInterval: 1,
                                      target: self,
                                      selector: #selector(handleTimerExecution(_:)),
                                      userInfo: userInfo,
                                      repeats: false)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, timer will fire only once.&lt;/p&gt;

&lt;p&gt;In Swift, we have an alternative approach to define a non-repeating task using GCD like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
   print("timer executed")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above method, you can see a scheduled task that will execute after 5 seconds from the current time. In the code, the now() function returns the current time. In the GCD approach, you don't need to invalidate the timer. RunLoop automatically invalidates the timer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Timer Tolerance &amp;amp; RunLoop Modes
&lt;/h2&gt;

&lt;p&gt;The timers work with RunLoop together. RunLoop is a fundamental part of threads and concurrency.&lt;/p&gt;

&lt;p&gt;When you schedule a timer, it's managed by a RunLoop. The run-loop keeps looping and executing tasks for repeating timers. When no tasks are left to execute, the run-loop waits or quits.&lt;/p&gt;

&lt;p&gt;An increasing number of timers in the app increases the risk of less responsiveness and more power usage. Because every timer fires at each second in the app.&lt;/p&gt;

&lt;p&gt;When we create a timer, we can easily set the tolerance property to reduce the power consumption of the app. Its default value is 0.0 but you can set this value in the 0-1 range.&lt;/p&gt;

&lt;p&gt;For example, you have created a repeating timer with the tolerance value 0.1. This means the system will fire the timer any time between the scheduled fire date and the scheduled fire date plus tolerance time (i.e. 0.1) — never before the scheduled fire date.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;timer?.tolerance = 0.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to utilise RunLoop?
&lt;/h3&gt;

&lt;p&gt;Basically, a RunLoop mode is a collection of input sources like touches, clicks, swiping, dragging, timer events etc.&lt;/p&gt;

&lt;p&gt;Each timer fires on the main thread and is attached to a RunLoop. We know that the main thread is also responsible for UI drawing, user interaction handling etc. Suppose the main thread is busy in timer execution then your UIs may be unresponsive.&lt;/p&gt;

&lt;p&gt;How to solve this problem?&lt;/p&gt;

&lt;p&gt;It's very simple to handle such problem. There are three types of RunLoop modes in iOS:&lt;/p&gt;

&lt;p&gt;default: It handles input sources that are not NSConnectionObjects.&lt;/p&gt;

&lt;p&gt;common: It handles a set of other run loop modes like timers, observers etc.&lt;/p&gt;

&lt;p&gt;tracking: It handles the UI responsiveness in the app.&lt;/p&gt;

&lt;p&gt;So common mode is good to use when we have multiple repeating timers in the app to avoid such problems. Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RunLoop.current.add(timer, forMode: .common)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Remember that, this is not needed for usual implementation. This line will be required in special cases which we explained above.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Points to remember:
&lt;/h2&gt;

&lt;p&gt;When your app enters the background, iOS pauses any running timers and when the app enters the foreground again, iOS resumes the timers.&lt;/p&gt;

&lt;p&gt;Each timer fires on the main thread and is attached to a RunLoop.&lt;/p&gt;

&lt;p&gt;Don't forget to stop the timer once the task is completed to reduce battery drain.&lt;/p&gt;

&lt;p&gt;When you use a timer, you can't be sure that it will be executed at the same time. But you can only be sure that you will get a callback according to timeInterval sets on the timer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Eat. Sleep. Swift Anytime. Repeat.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Async Image in SwiftUI</title>
      <dc:creator> Nafeez Ahmed</dc:creator>
      <pubDate>Mon, 26 Dec 2022 15:32:23 +0000</pubDate>
      <link>https://forem.com/nafeezahmed/async-image-in-swiftui-4i7c</link>
      <guid>https://forem.com/nafeezahmed/async-image-in-swiftui-4i7c</guid>
      <description>&lt;p&gt;Almost all the modern apps and websites are driven by images and videos. The images you see on shopping apps, social media and entertainment apps are all loaded from some backend source. Since there are a lot of backend calls needed for this, if you actually waiting for all those images to load before showing the website, it would take immense amount of time to load the apps - which would result in bad user experience.&lt;br&gt;
A very simple but efficient solution for that is loading the image asynchronously and showing a placeholder as the image till then. This is the exact approach used in almost all the apps where the images are asynchronously loaded and cached (in the best case scenerio).&lt;br&gt;
You can implement asynchronous image loading in SwiftUI for iOS 15+ using AsyncImage. Without further ado, let's take a look at it.&lt;/p&gt;
&lt;h2&gt;
  
  
  Basic Implementation
&lt;/h2&gt;

&lt;p&gt;The easiest way you can load an image with AsyncImage is with a URL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; AsyncImage(url: URL(string: "https://swiftanytime-content.s3.ap-south-1.amazonaws.com/SwiftUI-Beginner/Async-Image/TestImage.jpeg")) // 1       
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Result
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5r8h8vw458v12voa33lf.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5r8h8vw458v12voa33lf.jpg" alt="Image description" width="780" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Explanation:
&lt;/h2&gt;

&lt;p&gt;Async Image is initialized with a URL - which is initialized with a string.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note here, the image specific initializers like .resizeable() and .contentMode(aspectRatio:) cannot be used.&lt;br&gt;
For playing around with async image, feel free to use this test URL.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  AsyncImage with Image specific modifiers
&lt;/h1&gt;

&lt;p&gt;Many times, while working in production apps, using .resizable() and .aspectRatio(contentMode: ...) for images become inevitable. Like in the previous section, you can observe how the image was taking up all the available space. What if you don't want to change the content mode and resizeability in order to fix that? Let's try AsyncImage(url: ..., content: ..., placeholder: ...) initialization for that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  AsyncImage(url: URL(string: "https://swiftanytime-content.s3.ap-south-1.amazonaws.com/SwiftUI-Beginner/Async-Image/TestImage.jpeg")) { image in
            image
                .resizable()
                .aspectRatio(contentMode: .fill)

        } placeholder: {
            Color.gray
        }
        .frame(width: 250, height: 250)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Result
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj8bmq94012ppjbkpdafc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj8bmq94012ppjbkpdafc.gif" alt="Image description" width="780" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And in the case you only want to control the scale of the image, you can use the AsyncImage with the scale modifier. The default value of the scale is 1. Let's try using scale value of 2.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AsyncImage(url: URL(string: "https://swiftanytime-content.s3.ap-south-1.amazonaws.com/SwiftUI-Beginner/Async-Image/TestImage.jpeg")!, scale: 2)
            .frame(width: 250, height: 250)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Result
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk0vuevid2vh2naznmw7v.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk0vuevid2vh2naznmw7v.jpg" alt="Image description" width="780" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Image with placeholder while loading
&lt;/h1&gt;

&lt;p&gt;If a blank view while loading the image seems boring and less indicative, let's try to add a placeholder view like a progress spinner or an SF symbol.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; AsyncImage(url: URL(string: "https://swiftanytime-content.s3.ap-south-1.amazonaws.com/SwiftUI-Beginner/Async-Image/TestImage.jpeg")!) { image in
            image
                .resizable()
                .aspectRatio(contentMode: .fill)
        } placeholder: {
            Image(systemName: "photo.fill")
        }.frame(width: 250, height: 250)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Result
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjjolpxe16th1u0d5ybmm.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjjolpxe16th1u0d5ybmm.gif" alt="Image description" width="780" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Error handling for the images
&lt;/h1&gt;

&lt;p&gt;That was all about framing and adding placeholder to async image. But what if the image URL is invalid. SwiftUI allows us to use a view of your choice in the case the image URL is nil. For that, you can use the transaction parameter which allows you to handle the states of loading the image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  AsyncImage(url: URL(string: "AWrongURL")!) { phase in // 1 
            if let image = phase.image { // 2
                // if the image is valid
                image
                    .resizable()
                    .aspectRatio(contentMode: .fill)
            } else if phase.error != nil { // 3  
                // some kind of error appears
                Text("No image available")
            } else {
                //appears as placeholder image 
                Image(systemName: "photo") // 4
                    .resizable()
                    .aspectRatio(contentMode: .fill)
            }
        }.frame(width: 250, height: 250, alignment: .center)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result considering an invalid url was used:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdlf8s2g8k0r4fgojhzar.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdlf8s2g8k0r4fgojhzar.gif" alt="Image description" width="780" height="520"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code Explanation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Here, the async image is initialized and we have defined phase variable in the block. The phase variable changes as the image loads.The load phases can be - empty, success, error&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using an if let, the phase image is checked and displayed.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;3.Checking if the error is nil. If it is not, some text is displayed.&lt;/p&gt;

&lt;p&gt;4.Default image if none of the above conditions are satisfied.&lt;br&gt;
To understand image phases and transactions better, let's try another initializer - init(url:scale:transaction:content).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; AsyncImage(url: URL(string:"https://swiftanytime-content.s3.ap-south-1.amazonaws.com/SwiftUI-Beginner/Async-Image/TestImage.jpeg")!, scale: 2) { phase in // 1 
            if let image = phase.image { // 2 
                            // if the image is valid
                            image
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                        } else if phase.error != nil { // 3 
                            // some kind of error appears
                            Text("404! \n No image available 😢")
                                .bold()
                                .font(.title)
                                .multilineTextAlignment(.center)

                        } else { // 4 
                            // showing progress view as placeholder
                          ProgressView()
                                .font(.largeTitle)
                        }
        }.padding()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9doglxvv6nj16to0wmg9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9doglxvv6nj16to0wmg9.gif" alt="Image description" width="500" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the case you try entering a wrong URL, this is what the app shows.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fead5njl5ax38brfzn1hg.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fead5njl5ax38brfzn1hg.gif" alt="Image description" width="500" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code Explanation:&lt;br&gt;
1.An Async image is defined here with the scale of 2 and a test url. Along with that, a phase block is defined where you handle all the image states. You can observe that is the scale of the image is doubled in the result gif.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If the image phase is not nil, the image is presented in resizeable way and fill aspect ratio.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;3.If the error is not nil, some 404 text is shown.&lt;/p&gt;

&lt;p&gt;4.If neither of the earlier situations are called, just an activity indicator in large title style is shown.&lt;/p&gt;

&lt;p&gt;Congratulations! You deserve to celebrate this moment. Today you learned about Async Image implementation, adding a placeholder to it, using image-specific modifiers and error handling the URL.. We encourage you to read more related articles like Colors and Gradient in SwiftUI till then, have a great time ahead.&lt;/p&gt;

&lt;p&gt;Eat. Sleep. Swift Anytime. Repeat.&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
  </channel>
</rss>
