<?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: Ishaan Bedi</title>
    <description>The latest articles on DEV Community by Ishaan Bedi (@ishaanbedi).</description>
    <link>https://dev.to/ishaanbedi</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%2F453168%2F6852369d-7447-438a-96f1-3ff54f744cc4.jpeg</url>
      <title>DEV Community: Ishaan Bedi</title>
      <link>https://dev.to/ishaanbedi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ishaanbedi"/>
    <language>en</language>
    <item>
      <title>Understanding Strings in Swift</title>
      <dc:creator>Ishaan Bedi</dc:creator>
      <pubDate>Tue, 15 Feb 2022 15:55:05 +0000</pubDate>
      <link>https://dev.to/ishaanbedi/understanding-strings-in-swift-536j</link>
      <guid>https://dev.to/ishaanbedi/understanding-strings-in-swift-536j</guid>
      <description>&lt;p&gt;In this article, we'll be looking at the concept of Strings in Swift.&lt;/p&gt;

&lt;p&gt;Strings, as the name suggests, are a thread/collection of characters, woven together to form a word or a sentence.&lt;/p&gt;

&lt;p&gt;To initialize a string in Swift, we enclose the word or the sentence inside double-quotes. We may initialize string either as a variable or a constant, depending upon our need.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

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

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

&lt;/div&gt;



&lt;p&gt;In this code snippet, we have declared a constant &lt;em&gt;myName&lt;/em&gt; and we have stored a string value of "Ishaan" in it.&lt;/p&gt;

&lt;p&gt;Easy, as it gets :)&lt;/p&gt;

&lt;h3&gt;
  
  
  Multiline strings in Swift
&lt;/h3&gt;

&lt;p&gt;What if we want to store a poem or some text where we have to use multiple lines. The first syntax you'll guess in your mind would be something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let myPoem = "Hello,
We haven't talked in quite some time
I know
I haven't been the best
Of sons, hello, I've been traveling in the desert of my mind
And I Haven't found a drop"

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

&lt;/div&gt;



&lt;p&gt;But this is the wrong syntax and Swift will raise an error while compiling the code.&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644938020923%2FlssfH5X4r.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644938020923%2FlssfH5X4r.png" alt="Screenshot showing error"&gt;&lt;/a&gt;As you can see, Swift did not compile our code and showed us errors.&lt;/p&gt;

&lt;p&gt;The solution to this problem is to wrap our string inside three double-quotes instead of using double-quotes one time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let myPoem = 
""""
Hello,
We haven't talked in quite some time
I know
I haven't been the best
Of sons, hello, I've been traveling in the desert of my mind
And I Haven't found a drop
"""

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

&lt;/div&gt;



&lt;p&gt;Now, this is the perfect syntax and we have successfully created a multi-line string in Swift.&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644938315200%2FrMXVXg3wG6.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644938315200%2FrMXVXg3wG6.png" alt="Screenshot showing multi-line strings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And we can see that Swift compiled our code without any error this time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Including a double quote in our string
&lt;/h3&gt;

&lt;p&gt;What if we want to include double quotes inside our string, for example, we have to store a conversation inside our string.&lt;/p&gt;

&lt;p&gt;If we use our familiar syntax by wrapping our double quotes inside double quotes, Swift will get confused about which double quote is wrapping whom (too much confusion, I know). As you can see from the following screenshot as well that our program has not been compiled. &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644938780687%2FsT6KVCC1c.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644938780687%2FsT6KVCC1c.png" alt="Screenshot showing double quote error"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's get it sorted:&lt;/p&gt;

&lt;p&gt;To get double quotes inside our string, just put a backslash before the double quote inside the string, and you are good to go. &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644938931866%2FEOLk0AQms.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644938931866%2FEOLk0AQms.png" alt="Screenshot showing correct syntax for double quotes inside a string"&gt;&lt;/a&gt;As you can see from the above code snippet, we can see that after introducing backslash before double quotes, our program has successfully compiled and is working as per our satisfaction.&lt;/p&gt;

&lt;h3&gt;
  
  
  String interpolation in Swift
&lt;/h3&gt;

&lt;p&gt;The next crucial part in understanding strings in Swift is understanding the concept of &lt;em&gt;String Interpolation&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To interpolate a variable or a constant inside a string, we use a backslash followed by a variable or a constant wrapped inside a pair of parenthesis, and it's done.&lt;/p&gt;

&lt;p&gt;Let's look at the following code snippet to understand better.&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644939550531%2F4hdpkOGaQ.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644939550531%2F4hdpkOGaQ.png" alt="Screenshot showing string interpolation example."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, we have interpolated strings inside a variable &lt;em&gt;myDetails&lt;/em&gt; by including the constant &lt;em&gt;myName&lt;/em&gt; &amp;amp; the variable &lt;em&gt;myAge&lt;/em&gt; in it.&lt;/p&gt;

&lt;p&gt;We can insert any kind of raw Swift code inside &lt;code&gt;\( )&lt;/code&gt;, be it be calculations, values of any kind of data type, or a reference from another variable/constant. You may see below code snippets as an example for this concept:&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644940020031%2FQPf6VlIzR3.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644940020031%2FQPf6VlIzR3.png" alt="Screenshot showing string interpolation example"&gt;&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644940087731%2FJB-LisXDq.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1644940087731%2FJB-LisXDq.png" alt="Screenshot showing string interpolation example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, that's all for this article. Hope you've got a deep understanding of strings in Swift. We'll be looking at some advanced string methods and operations in an upcoming article.&lt;/p&gt;

&lt;p&gt;Thank you for reading this far.&lt;/p&gt;

&lt;p&gt;Check out more at ishaanbedi.me&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Understanding Constants in Swift</title>
      <dc:creator>Ishaan Bedi</dc:creator>
      <pubDate>Fri, 04 Feb 2022 06:21:47 +0000</pubDate>
      <link>https://dev.to/ishaanbedi/understanding-constants-in-swift-1ah1</link>
      <guid>https://dev.to/ishaanbedi/understanding-constants-in-swift-1ah1</guid>
      <description>&lt;p&gt;In today's article, we'll be looking at the concept of Constants in Swift.&lt;/p&gt;

&lt;p&gt;I have already posted an article on Variables in Swift, check it out &lt;a href="https://dev.to/ishaanbedi/understanding-variables-in-swift-48p4"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Just like variables, the concept of constants is also very easy to understand and implement.&lt;/p&gt;

&lt;p&gt;Let's dive deeper and understand constants in-depth:&lt;/p&gt;

&lt;h3&gt;
  
  
  What are constants?
&lt;/h3&gt;

&lt;p&gt;Just like its name, constants are something that remains constant throughout the program!&lt;/p&gt;

&lt;p&gt;Once a value is declared as a constant in the memory, its value cannot be changed throughout the execution of your program or application.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to initialize a constant?
&lt;/h3&gt;

&lt;p&gt;To declare a constant, we use the &lt;code&gt;let&lt;/code&gt; keyword, followed by the name we want to give to that particular constant and assign it to any kind of data type we want to be constant throughout the execution of the program.&lt;/p&gt;

&lt;p&gt;It's just like initializing a variable, but replacing the &lt;code&gt;var&lt;/code&gt; with the &lt;code&gt;let&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;We can store any kind of data type as a constant, whether it may be an integer, float, double, string, boolean, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's look at an example:
&lt;/h3&gt;



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

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

&lt;/div&gt;



&lt;p&gt;According to this code snippet, we declare a new constant called myName that stores the string &lt;em&gt;' Ishaan'&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Do remember to use the keyword &lt;code&gt;let&lt;/code&gt;, otherwise, your program will generate an error.&lt;/p&gt;

&lt;p&gt;Now, myName, a constant has a value of 'Ishaan' in the memory, which will remain constant throughout the flow of our program.&lt;/p&gt;

&lt;p&gt;You may try implementing this on your own on a Swift Playground or some other compiler and explore new concepts and errors.&lt;/p&gt;

&lt;p&gt;Now, if we try to change the value of the constant &lt;em&gt;myName&lt;/em&gt;, Swift will show us an error, as shown in the image below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---RMlSJiN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1643954160188/pc5q1eAYo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---RMlSJiN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1643954160188/pc5q1eAYo.png" alt="Image showing the error" width="800" height="551"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the second attempt of initialization, Swift throws an error that it cannot assign &lt;em&gt;myName&lt;/em&gt; to a new value as it is a constant.&lt;/p&gt;

&lt;p&gt;It also shows us a hint to solve this problem by replacing &lt;code&gt;let&lt;/code&gt; with &lt;code&gt;var&lt;/code&gt;. In other words, it's asking us to declare myName as a variable if we need to change its value at any point of time, later in the program.&lt;/p&gt;

&lt;h3&gt;
  
  
  Some examples with other data types:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let isAdult = true
print(isAdult)

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

&lt;/div&gt;



&lt;p&gt;In this example, we store a boolean value as a constant in &lt;em&gt;isAdult&lt;/em&gt; and then we print it. As expected, we will get &lt;code&gt;true&lt;/code&gt; as the output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let myNumber = 13
print(myNumber)

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

&lt;/div&gt;



&lt;p&gt;In this example, we store an integral value of 13 as a constant in &lt;em&gt;myNumber&lt;/em&gt; and then we print it. As expected, we will get &lt;code&gt;13&lt;/code&gt; as the output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mathScore = 98.5
print(mathScore)

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

&lt;/div&gt;



&lt;p&gt;In this example, we store a floating value of 98.5 as a constant in &lt;em&gt;mathScore&lt;/em&gt; and then we print it. As expected, we will get &lt;code&gt;98.5&lt;/code&gt; as the output.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Good to know: Just like a variable, we can also explicitly declare a constant's data type to define what kind of data we want it to store in it.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mathScore:Int
mathScore = 100
print(mathScore)

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

&lt;/div&gt;



&lt;p&gt;Here, we are explicitly telling the compiler to make sure that the constant mathScore stores an integer value only, though only one time we'll be doing it (anywhere in the program).&lt;/p&gt;

&lt;p&gt;You may avoid explicitly declaring data type unless it's absolutely necessary.&lt;/p&gt;

&lt;p&gt;So that's all for this article. You now have a deep understanding of constants in Swift. Hope you have understood what I wanted to convey.&lt;/p&gt;

&lt;p&gt;Thank you for reading this far.&lt;/p&gt;

&lt;p&gt;Check out more at ishaanbedi.me&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Understanding Variables in Swift</title>
      <dc:creator>Ishaan Bedi</dc:creator>
      <pubDate>Mon, 31 Jan 2022 05:28:53 +0000</pubDate>
      <link>https://dev.to/ishaanbedi/understanding-variables-in-swift-48p4</link>
      <guid>https://dev.to/ishaanbedi/understanding-variables-in-swift-48p4</guid>
      <description>&lt;p&gt;In today's article, we'll be looking at the concept of variables in Swift.&lt;/p&gt;

&lt;p&gt;Variables, in any programming language, is the concept that a programmer learns about in the initial days of learning that language, and understanding it is an important part of gaining a solid grip over that particular language.&lt;/p&gt;

&lt;p&gt;Talking about variables in Swift, the concept of variables is very easy to understand and implement.&lt;/p&gt;

&lt;p&gt;Let's dive deeper and understand variables in depth.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are variables?
&lt;/h3&gt;

&lt;p&gt;As the name suggests, &lt;em&gt;variables&lt;/em&gt; are something that can &lt;em&gt;vary&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A variable refers to a storage location we reserve in the computer’s memory for some data or value to store that we can change freely &amp;amp; manipulate as per our program's requirements.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to initialize a variable?
&lt;/h3&gt;

&lt;p&gt;To declare a variable, we use the &lt;code&gt;var&lt;/code&gt; keyword, followed by the name we want to give to that particular variable and assign it to something we want this particular variable to store in the memory.&lt;/p&gt;

&lt;p&gt;We can store any kind of data type as a variable, whether it may be an integer, float, double, string, boolean, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's look at an example:
&lt;/h3&gt;



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

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

&lt;/div&gt;



&lt;p&gt;According to this code snippet, we declare a new variable called &lt;em&gt;myNumber&lt;/em&gt; that stores the integer value of &lt;em&gt;13&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Do remember to use the keyword var, otherwise, your program will generate an error.&lt;/p&gt;

&lt;p&gt;Now, &lt;em&gt;myNumber&lt;/em&gt;, a variable has a value of &lt;em&gt;13&lt;/em&gt; in the memory.&lt;/p&gt;

&lt;p&gt;You may try implementing this on your own on a Swift Playground or some other compiler.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Pp_SNJC---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1643603285177/Mm6V6q5QI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Pp_SNJC---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1643603285177/Mm6V6q5QI.png" alt="Variables Image 1" width="800" height="235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can perform any kind of operations on this variable like mathematical calculations, building &amp;amp; solving logic, implementing algorithms, etc, just thinking of it as a substitute for the integer 13.&lt;/p&gt;

&lt;p&gt;And as we discussed above, we can change or manipulate this value.&lt;br&gt;
&lt;/p&gt;

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

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

&lt;/div&gt;



&lt;p&gt;No need to use the &lt;code&gt;var&lt;/code&gt; keyword again as we are trying to modify an existing variable that is already declared earlier.&lt;/p&gt;

&lt;p&gt;Now the variable &lt;code&gt;myNumber&lt;/code&gt; has the value of integer &lt;em&gt;26&lt;/em&gt; in the memory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oGR5r1Sb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1643603438967/WHveEJKze.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oGR5r1Sb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1643603438967/WHveEJKze.png" alt="Variables Image 2" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, after reassigning the variable to 26, the second &lt;code&gt;print(myNumber)&lt;/code&gt; statement prints out 26 in the console.&lt;/p&gt;

&lt;h3&gt;
  
  
  IMPORTANT NOTE
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;If we are reassigning the variable to a new value, the new value should be of the same data type of which the variable was earlier declared for:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

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

myAge = 19

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

&lt;/div&gt;



&lt;p&gt;The above code snippet is valid in the Swift program as we are reassigning an integer 19 to the variable, which was earlier initialized with a value of integer 18. The data type at both times of initialization was an integer, making this program valid.&lt;/p&gt;

&lt;p&gt;Whereas in the following code snippet, an error will be generated:&lt;br&gt;
&lt;/p&gt;

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

myAge = "eighteen"

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qKDtKo8K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1643604767552/76DkR1d45.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qKDtKo8K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1643604767552/76DkR1d45.png" alt="Error Image 1" width="800" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the Swift compiler shows us an error that it cannot assign a variable of type string to a variable that was earlier assigned an integral value.&lt;/p&gt;

&lt;p&gt;Keep this in mind, and you are good to go :)&lt;/p&gt;

&lt;h3&gt;
  
  
  Some examples with other data types:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name = "John Doe"
print(name)

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

&lt;/div&gt;



&lt;p&gt;In this example, we store a variable of type string &lt;em&gt;"John Doe"&lt;/em&gt; in the variable name and then we print it. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XXc2EBhW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1643603933484/07_aiNlBr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XXc2EBhW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1643603933484/07_aiNlBr.png" alt="Variables Image 3" width="800" height="310"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

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

var isStudent = true

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

&lt;/div&gt;



&lt;p&gt;In this example, we initialize two variables and store a boolean value in them.&lt;br&gt;
&lt;/p&gt;

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

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

&lt;/div&gt;



&lt;p&gt;In this example, we store a floating value in the variable &lt;code&gt;myScore&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You may try more examples on your own by storing and experimenting with other data types.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Good to know: We can explicitly declare a variable's data type to define what kind of data we want it to store in it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myAge:Int
myAge = 18

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

&lt;/div&gt;



&lt;p&gt;Here, we are explicitly telling the compiler to make sure that the variable&lt;code&gt;myAge&lt;/code&gt; always stores an integer value only.&lt;/p&gt;

&lt;p&gt;So now you also have an idea of explicitly declaring the data type of a variable.&lt;/p&gt;

&lt;p&gt;Since we already know that if we directly initialize a variable to some data type, it'll always store that kind of data type only in future re-assignments, you may avoid explicitly declaring data type unless it's absolutely necessary.&lt;/p&gt;

&lt;p&gt;So that's all for this article. You now have a deep understanding of variables in Swift. Hope you have understood what I wanted to convey.&lt;/p&gt;

&lt;p&gt;Thank you for reading this far.&lt;/p&gt;

&lt;p&gt;Check out more at ishaanbedi.me&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Remove Duplicate Elements From An Array With Just a Single Line Of Code!</title>
      <dc:creator>Ishaan Bedi</dc:creator>
      <pubDate>Fri, 28 Jan 2022 05:55:25 +0000</pubDate>
      <link>https://dev.to/ishaanbedi/remove-duplicate-elements-from-an-array-with-just-a-single-line-of-code-4k19</link>
      <guid>https://dev.to/ishaanbedi/remove-duplicate-elements-from-an-array-with-just-a-single-line-of-code-4k19</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;In today's article, we'll be looking at a super simple trick that can be used to remove duplicate elements from an array in Javascript.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With the help of this trick, you can delete the duplicate elements from an array without the use of loops or other complex operations.&lt;/p&gt;

&lt;p&gt;We'll be using the concept of Sets in Javascript to achieve this.&lt;/p&gt;

&lt;p&gt;So let's start...&lt;/p&gt;

&lt;p&gt;We start with initializing a new example array, on which we’ll perform the operation to remove duplicate elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arr = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5];

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

&lt;/div&gt;



&lt;p&gt;In this example array, we have duplicate integers (as many times as their value). We want to remove duplicate elements from this array and our updated array should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 2, 3, 4, 5]

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

&lt;/div&gt;



&lt;p&gt;Now to remove duplicate elements, the simplest part, we use a combination of arrays’ spread operator (to spread the elements of the array) along with creating a new Set which will allow us to store only the unique elements from the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        arr = [... new Set(arr)];

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

&lt;/div&gt;



&lt;p&gt;This re-initializes the variable arr with the non-duplicate elements, just like the way we want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Arr -&amp;gt; [1, 2, 3, 4, 5]

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

&lt;/div&gt;



&lt;p&gt;If you want to retain the original array, you may declare a new variable that stores the non-duplicate elements array from the original array.&lt;/p&gt;

&lt;p&gt;Ex.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arr = [1, 2, 2, 3, 3, 3];
var newArr = [... new Set(arr)];

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

&lt;/div&gt;



&lt;p&gt;Through this way, we get a new array with no duplicate elements, and we also have our original array, which we might need later.&lt;/p&gt;

&lt;p&gt;Arr -&amp;gt; [1, 2, 2, 3, 3, 3]&lt;/p&gt;

&lt;p&gt;newArr -&amp;gt; [1, 2, 3]&lt;/p&gt;

&lt;p&gt;Not only integers, but we can use this concept for any data type. Let’s take an example with elements of type String.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var stringArray = ['a', 'b', 'b', 'c', 'd', 'd', 'd', 'e', 'e', 'f'];

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

&lt;/div&gt;



&lt;p&gt;Now, using the same concept as discussed above, we use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stringArray = [... new Set(stringArray)];

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

&lt;/div&gt;



&lt;p&gt;And, as you guessed, the output after removing all the duplicate elements from the array is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
["a", "b", "c", "d", "e", "f"]

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

&lt;/div&gt;



&lt;p&gt;The last example we take is with an array of boolean values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var boolArray = [true,true,false,false,true,false];

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

&lt;/div&gt;



&lt;p&gt;Applying the same concept, we use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;boolArray = [... new Set(boolArray)];

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

&lt;/div&gt;



&lt;p&gt;And as expected, we have the boolArray with only two elements, ie,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[true, false]

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  The final key point:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The Set() constructor lets us create Set Objects that lets us store the unique value of any data type.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thank you for reading this far. Check out more at &lt;a href="http://www.ishaanbedi.me"&gt;www.ishaanbedi.me&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
