<?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>DEV Community: Jyothish Johnson</title>
    <description>The latest articles on DEV Community by Jyothish Johnson (@jyothishjohnson).</description>
    <link>https://dev.to/jyothishjohnson</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%2F193905%2F46e5c72b-4df0-4b0c-acff-d973006ac5cd.jpeg</url>
      <title>DEV Community: Jyothish Johnson</title>
      <link>https://dev.to/jyothishjohnson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jyothishjohnson"/>
    <language>en</language>
    <item>
      <title>Widgets: An Introduction</title>
      <dc:creator>Jyothish Johnson</dc:creator>
      <pubDate>Thu, 24 Apr 2025 07:09:34 +0000</pubDate>
      <link>https://dev.to/jyothishjohnson/widgets-an-introduction-5a02</link>
      <guid>https://dev.to/jyothishjohnson/widgets-an-introduction-5a02</guid>
      <description>&lt;p&gt;There are times when you would want to show the apps contents/data in an helpful way upfront to the user, even when the user is not actively using your app. Widgets(iOS 14+) and Live Activities(iOS 16.1+) are the possible ways to do this. &lt;/p&gt;

&lt;p&gt;For this article, lets start with adding a basic widget to our app.&lt;/p&gt;




&lt;h3&gt;
  
  
  First thing first, let's add the Widget Extension to the app.
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;File → New → Target → Widget Extension&lt;/li&gt;
&lt;li&gt;Give a name to the extension → Finish&lt;/li&gt;
&lt;/ol&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%2Fm6lpc0o96tk1dv7acbd6.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%2Fm6lpc0o96tk1dv7acbd6.png" alt="File → New → Target → Widget Extension" width="800" height="463"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you will be able to see files related to a basic Widget added to your project. This template widget shows &lt;strong&gt;time&lt;/strong&gt;. You should be able to see this in a struct that conforms to &lt;code&gt;Widget&lt;/code&gt; protocol. This Widget implementation’s &lt;strong&gt;&lt;code&gt;body&lt;/code&gt;&lt;/strong&gt; needs a type of &lt;code&gt;WidgetConfiguration.&lt;/code&gt; &lt;/p&gt;




&lt;p&gt;There are mainly three types of &lt;code&gt;WidgetConfiguration&lt;/code&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;StaticConfiguration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Used in static widgets, the data cannot be customized by the user.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;AppIntentConfiguration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;User can configure the data populated inside this widget. Ex: Widget to track a events from a particular calendar account when user has multiple calendar accounts.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;ActivityConfiguration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is exclusively used for Live Activities.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To use multiple widget types in the app use &lt;code&gt;WidgetBundle&lt;/code&gt; protocol.&lt;/p&gt;




&lt;h3&gt;
  
  
  Providing Timelines for the data
&lt;/h3&gt;

&lt;p&gt;Both &lt;strong&gt;StaticConfiguration&lt;/strong&gt; and &lt;strong&gt;AppIntentConfiguration&lt;/strong&gt;, need a timeline provider. For this we need an implementation that conforms to &lt;code&gt;TimelineProvider&lt;/code&gt;. There are few mandatory functions that we need to implement in order to conform to above protocol.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;func placeholder(in: Self.Context) -&amp;gt; Self.Entry&lt;/code&gt;  → Used to provide a placeHolder widget view, which might be used when the user sees the widget for the first time. Think of it as a generic view to give an idea to the user about your widget.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;func getSnapshot(in: Self.Context, completion: (Self.Entry) -&amp;gt; Void)&lt;/code&gt; → This function can be used to get a quick info about the widget in temporary states. (ie: In Widget gallery - &lt;code&gt;context.isPreview = true&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;func getTimeline(in: Self.Context, completion: (Timeline&amp;lt;Self.Entry&amp;gt;) -&amp;gt; Void)&lt;/code&gt;  → This is where we can provide the information to the app, regarding when to update the widget information. We can pass a series of future dates for this. We can also specify a policy regarding when the timeline should be refreshed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TimelineEntry&lt;/code&gt; → The data that the widget shows needs to confirm to this protocol.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This was a basic introduction to adding the Widget capability to your app. Further we will explore more complex implementations.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>widgets</category>
      <category>swift</category>
      <category>xcode</category>
    </item>
    <item>
      <title>JSON basics, encoding and decoding with Swift</title>
      <dc:creator>Jyothish Johnson</dc:creator>
      <pubDate>Wed, 11 Oct 2023 09:47:11 +0000</pubDate>
      <link>https://dev.to/jyothishjohnson/json-basics-encoding-and-decoding-with-swift-1hgp</link>
      <guid>https://dev.to/jyothishjohnson/json-basics-encoding-and-decoding-with-swift-1hgp</guid>
      <description>&lt;p&gt;This is a first part of a beginner series, where we understand basic of networking in Swift.&lt;/p&gt;

&lt;p&gt;To simplify it greatly, &lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/JSON"&gt;JSON&lt;/a&gt;(Javascript Object Notation)&lt;/strong&gt; is the way most clients and servers, share data with each other.&lt;/p&gt;

&lt;p&gt;Here is a sample json, that fetches list of followers for a users profile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"userId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"123456"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"userName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"John Doe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"followers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"followerId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"789012"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"followerUsername"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"follower1_username"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"followerName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Follower One"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"followerId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"345678"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"followerUsername"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"follower2_username"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"followerName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Follower Two"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"followerId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"901234"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"followerUsername"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"follower3_username"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"followerName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Follower Three"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

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

&lt;/div&gt;






&lt;h3&gt;
  
  
  JSON Decoding
&lt;/h3&gt;

&lt;p&gt;One of the most basic things you will do as part of networking in Swift, is displaying the data received from server into some UI. Lets take the above JSON as an example. To do this we have to decode the JSON response into a object in Swift that we can reason about. &lt;/p&gt;

&lt;p&gt;Firstly we will create a &lt;code&gt;struct&lt;/code&gt; in Swift that can be mapped to the json response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;//1&lt;/span&gt;
&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Decodable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//2&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;followers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Follower&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;Follower&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Decodable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;followerId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;followerUsername&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;followerName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note&lt;/strong&gt;: Here we are using exact same keys as in the json response. More about custom keys in a later article.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The struct we created is confirming to &lt;code&gt;Decodable&lt;/code&gt; protocol, which is required for the json to be decoded to a Swift object.&lt;/li&gt;
&lt;li&gt;If you notice, all the properties we used here have the same key with the json. This is important as the mapping happens between same keys(case-sensitive). Of-course we can customise the keys based on our needs, this will be covered in a later article.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now lets write the code to decode this json.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;//1&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;json&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"""
{
  "&lt;/span&gt;&lt;span class="n"&gt;userId&lt;/span&gt;&lt;span class="s"&gt;": "&lt;/span&gt;&lt;span class="mi"&gt;123456&lt;/span&gt;&lt;span class="s"&gt;",
  "&lt;/span&gt;&lt;span class="n"&gt;userName&lt;/span&gt;&lt;span class="s"&gt;": "&lt;/span&gt;&lt;span class="kt"&gt;John&lt;/span&gt; &lt;span class="kt"&gt;Doe&lt;/span&gt;&lt;span class="s"&gt;",
  "&lt;/span&gt;&lt;span class="n"&gt;followers&lt;/span&gt;&lt;span class="s"&gt;": [
    {
      "&lt;/span&gt;&lt;span class="n"&gt;followerId&lt;/span&gt;&lt;span class="s"&gt;": "&lt;/span&gt;&lt;span class="mi"&gt;789012&lt;/span&gt;&lt;span class="s"&gt;",
      "&lt;/span&gt;&lt;span class="n"&gt;followerUsername&lt;/span&gt;&lt;span class="s"&gt;": "&lt;/span&gt;&lt;span class="n"&gt;follower1_username&lt;/span&gt;&lt;span class="s"&gt;",
      "&lt;/span&gt;&lt;span class="n"&gt;followerName&lt;/span&gt;&lt;span class="s"&gt;": "&lt;/span&gt;&lt;span class="kt"&gt;Follower&lt;/span&gt; &lt;span class="kt"&gt;One&lt;/span&gt;&lt;span class="s"&gt;"
    },
    {
      "&lt;/span&gt;&lt;span class="n"&gt;followerId&lt;/span&gt;&lt;span class="s"&gt;": "&lt;/span&gt;&lt;span class="mi"&gt;345678&lt;/span&gt;&lt;span class="s"&gt;",
      "&lt;/span&gt;&lt;span class="n"&gt;followerUsername&lt;/span&gt;&lt;span class="s"&gt;": "&lt;/span&gt;&lt;span class="n"&gt;follower2_username&lt;/span&gt;&lt;span class="s"&gt;",
      "&lt;/span&gt;&lt;span class="n"&gt;followerName&lt;/span&gt;&lt;span class="s"&gt;": "&lt;/span&gt;&lt;span class="kt"&gt;Follower&lt;/span&gt; &lt;span class="kt"&gt;Two&lt;/span&gt;&lt;span class="s"&gt;"
    },
    {
      "&lt;/span&gt;&lt;span class="n"&gt;followerId&lt;/span&gt;&lt;span class="s"&gt;": "&lt;/span&gt;&lt;span class="mi"&gt;901234&lt;/span&gt;&lt;span class="s"&gt;",
      "&lt;/span&gt;&lt;span class="n"&gt;followerUsername&lt;/span&gt;&lt;span class="s"&gt;": "&lt;/span&gt;&lt;span class="n"&gt;follower3_username&lt;/span&gt;&lt;span class="s"&gt;",
      "&lt;/span&gt;&lt;span class="n"&gt;followerName&lt;/span&gt;&lt;span class="s"&gt;": "&lt;/span&gt;&lt;span class="kt"&gt;Follower&lt;/span&gt; &lt;span class="kt"&gt;Three&lt;/span&gt;&lt;span class="s"&gt;"
    }
  ]
}
"""&lt;/span&gt;

&lt;span class="c1"&gt;//2&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;using&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;//3&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;userObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="kt"&gt;JSONDecoder&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;userObj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Decoding failed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Error getting json data"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;We will use Swift's multiline string literals to store json into a property.&lt;/li&gt;
&lt;li&gt;This line converts the json string into a &lt;code&gt;Data&lt;/code&gt; object where the characters of the string are encoded using UTF-8.&lt;/li&gt;
&lt;li&gt;This line uses the instance method of &lt;code&gt;JSONDecoder&lt;/code&gt; to specify the type of the object(ie. &lt;code&gt;User&lt;/code&gt;) that we want and passing the &lt;code&gt;data&lt;/code&gt; for decoding.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MnsRO_6Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lenf3v06wcplzbbcsny0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MnsRO_6Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lenf3v06wcplzbbcsny0.png" alt="Xcode console response for User object" width="800" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can see the &lt;code&gt;User&lt;/code&gt; object being printed in Xcode console.&lt;/p&gt;

&lt;p&gt;This can help you get started with displaying data received from your server.&lt;/p&gt;




&lt;h3&gt;
  
  
  JSON Encoding
&lt;/h3&gt;

&lt;p&gt;Most of the times, we also have to send some or the other data back to the server. For example, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User searches something in your app&lt;/li&gt;
&lt;li&gt;User authentication&lt;/li&gt;
&lt;li&gt;Save user data to server&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the above cases, you will have to send some form of data as part of your &lt;code&gt;httpBody&lt;/code&gt;. In these kind of scenarios we have to convert our Swift object to an encoded data. Lets look into how we can do that.&lt;/p&gt;

&lt;p&gt;First things first, we have to update our User struct to be encodable. For this we have to conform to the &lt;code&gt;Encodable&lt;/code&gt; protocol. But as we already have a conformance to &lt;code&gt;Decodable&lt;/code&gt;, there is an easier way to do this: &lt;code&gt;Codable&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Codable&lt;/code&gt; is literally conbination of &lt;code&gt;Encodable&lt;/code&gt; and &lt;code&gt;Decodable&lt;/code&gt; protocols.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;typealias&lt;/span&gt; &lt;span class="kt"&gt;Codable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Decodable&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="kt"&gt;Encodable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Updated &lt;code&gt;User struct&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Codable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;followers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Follower&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;Follower&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Codable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;followerId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;followerUsername&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;followerName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Now lets see how to encode the User object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//1&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;encodedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="kt"&gt;JSONEncoder&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//2&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;jsonString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;encodedData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;encoding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jsonString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;We pass the &lt;code&gt;User&lt;/code&gt; object to be encoded to &lt;code&gt;JSONEncoder&lt;/code&gt;'s &lt;code&gt;encode&lt;/code&gt; function. If encoding is successful we will get encoded &lt;code&gt;Data&lt;/code&gt;, which we use based on our needs (ie. Send to server via the api, or print the encoded String as we do here)&lt;/li&gt;
&lt;li&gt;We pass the encoded data to &lt;code&gt;String&lt;/code&gt;'s initializer and print the json string which is returned. This json string is exactly same as the json that we created in the beginning.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Console response below 👇&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7kvQMJxW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6j049kwo4hmbol1564ho.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7kvQMJxW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6j049kwo4hmbol1564ho.png" alt="Xcode console response for encoded json string" width="800" height="299"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thats a wrap on basics of encoding and decoding json in Swift!&lt;/p&gt;

&lt;p&gt;All the code available in Github &lt;a href="https://gist.github.com/jyothishjohnson/652c31fc627059de0efdaf003460174d"&gt;gist&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;🗨️💡 &lt;em&gt;Join the Conversation! Share Your Thoughts Below.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;🗣️ &lt;em&gt;Your opinion matters! Eager to hear your take on this topic. Don't hold back—let's dive into a lively discussion!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Enjoyed this Article?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;💖 &lt;em&gt;React: Click the heart icon to show your appreciation and help others discover this content too!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;🔔 &lt;em&gt;Follow for more Swift/iOS content!&lt;/em&gt; &lt;a href="https://twitter.com/jyo_johnson"&gt;@jyo_johnson on X(Twitter)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🚀 &lt;em&gt;Share: Spread the knowledge! Share this article with your network.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Your engagement is what keeps our community thriving. Thank you for being a part of it!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>swift</category>
      <category>json</category>
      <category>beginners</category>
      <category>ios</category>
    </item>
  </channel>
</rss>
