<?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: Mayank Gupta👨‍💻🥑</title>
    <description>The latest articles on DEV Community by Mayank Gupta👨‍💻🥑 (@iosmayank).</description>
    <link>https://dev.to/iosmayank</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%2F860899%2Ff49e1234-3872-46bf-ba72-e8f619940b82.jpg</url>
      <title>DEV Community: Mayank Gupta👨‍💻🥑</title>
      <link>https://dev.to/iosmayank</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iosmayank"/>
    <language>en</language>
    <item>
      <title>Swift if else, switch case Conditional Statements</title>
      <dc:creator>Mayank Gupta👨‍💻🥑</dc:creator>
      <pubDate>Mon, 23 May 2022 12:11:18 +0000</pubDate>
      <link>https://dev.to/iosmayank/swift-if-else-switch-case-conditional-statements-akl</link>
      <guid>https://dev.to/iosmayank/swift-if-else-switch-case-conditional-statements-akl</guid>
      <description>&lt;p&gt;``To put it simply, a group of codes, when executed on a particular/certain conditions, are known as conditional statements. You can quickly identify them by figuring out some noticeable terms like &lt;strong&gt;if&lt;/strong&gt;, &lt;strong&gt;if ...else&lt;/strong&gt;, &lt;strong&gt;switch case&lt;/strong&gt;. Generally, developers use it when needed to execute the code on a particular condition, and that could be any like, for example,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;if the value is more significant, lower, or equal to&lt;/li&gt;
&lt;li&gt;if the specified condition is true/false, etc. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Read and go over the detailed explanation below to better understand each type.&lt;/p&gt;

&lt;h2&gt;
  
  
  IF... ELSE Statements
&lt;/h2&gt;

&lt;p&gt;Understand this example before jumping towards the main content. So consider giving someone a contract for painting a house, and in return, they will keep one condition to start working with you. The condition is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;He/she will not work on Sundays&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;`&lt;code&gt;&lt;/code&gt;swift&lt;br&gt;
/* IF Sunday&lt;br&gt;
Holiday&lt;br&gt;
ELSE&lt;br&gt;
Working Day */&lt;/p&gt;

&lt;p&gt;//Conditional Statements works like this&lt;br&gt;
&lt;code&gt;&lt;/code&gt;` &lt;/p&gt;

&lt;p&gt;To write it down in the code format, you have to start with the term "if" and the just like functions, add open and close braces, and end with parenthesis.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`swift&lt;br&gt;
var day = "Sunday"&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;`&lt;code&gt;&lt;/code&gt;swift&lt;br&gt;
if (day == "Sunday") {  &lt;/p&gt;

&lt;p&gt;//Note: var day = "Sunday" is already declared but outside this conditional statement&lt;/p&gt;

&lt;p&gt;print("Holiday")&lt;/p&gt;

&lt;p&gt;} else {&lt;/p&gt;

&lt;p&gt;print("Working day")&lt;/p&gt;

&lt;p&gt;}&lt;br&gt;
&lt;code&gt;&lt;/code&gt;`&lt;/p&gt;

&lt;p&gt;The variable day has a value called "Sunday". Remember that never use only one "=". It should be "==" because it compares our condition with the given value and only works afterward.&lt;/p&gt;

&lt;p&gt;Now the question is: What happens if there are more conditions?&lt;/p&gt;

&lt;p&gt;Answer. We use "Operators" for our conditional statements. &lt;br&gt;
They are mainly two kinds of operators, "OR" and "AND" denoted as "||" and "&amp;amp;&amp;amp;" respectively.  &lt;/p&gt;

&lt;p&gt;Again try understanding this with the same example but with more conditions.&lt;/p&gt;

&lt;p&gt;This time the conditions are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Holiday on Sundays.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Also, off-day on National Holidays.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As you are familiar with simple IF...ELSE, directly understand the code format:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`swift&lt;br&gt;
var holiday = "National"&lt;br&gt;
if (day == "Sunday" || holiday == "National") {&lt;br&gt;
    print("Off day")&lt;br&gt;
} else {&lt;br&gt;
    print("Working day")&lt;br&gt;
}&lt;br&gt;
`&lt;/code&gt;&lt;br&gt;
If you see here, you use the "OR" operator because either condition should be true. When you press command+R, the program will run and go through this condition to check whether the given condition is true with the value you entered, but here either should be true to print "Off day" unlike the first example. Otherwise, it will print "Working day".&lt;/p&gt;

&lt;p&gt;Now add some more toppings to the example. So, after five years of working in a position, a person tells his/her boss that he/she will only be having off-day on Sunday if there is a national holiday.&lt;/p&gt;

&lt;p&gt;The condition for that would be:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`swift&lt;br&gt;
if (day == "Sunday" &amp;amp;&amp;amp; holiday == "National") {&lt;br&gt;
    print("Off day")&lt;br&gt;
} else {&lt;br&gt;
    print("Working day")&lt;br&gt;
}&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, if you see the condition, you use the "AND" operator. This is because you are putting two needs simultaneously, and both should be true to take a holiday on Sunday. Otherwise, you have to go to work.&lt;/p&gt;

&lt;p&gt;In technical terms, it will print "Off day" if the condition is proper, otherwise "Working day".&lt;/p&gt;

&lt;h2&gt;
  
  
  IF... ELSE IF Statements
&lt;/h2&gt;

&lt;p&gt;Continue understanding this concept as well with the real-life example. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The person is hustling so hard that he now keeps one more condition that he/she needs a vacation in June.&lt;/li&gt;
&lt;li&gt;Additionally, a holiday on Sunday if there is a National Holiday.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There you write:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`swift&lt;br&gt;
var month = "June"&lt;br&gt;
if (day == "Sunday" &amp;amp;&amp;amp; holiday == "National") {&lt;br&gt;
    print("Off day")&lt;br&gt;
} else if (month == "June") {&lt;br&gt;
    print("Vacation")&lt;br&gt;
} else {&lt;br&gt;
    print("Working day")&lt;br&gt;
}&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Switch Statements / Switch Case
&lt;/h2&gt;

&lt;p&gt;They work pretty much similar to if ...else condition but syntax and way of working are slightly different.&lt;/p&gt;

&lt;p&gt;Let's declare a variable,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`swift&lt;br&gt;
var event = "Apple"&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Write down the keyword "switch" to start with the switch case. Then select the variable declared "event" and then a parenthesis.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;`swift&lt;br&gt;
switch event {&lt;br&gt;
case "Apple":&lt;br&gt;
    print("WWDC")&lt;br&gt;
case "Google":&lt;br&gt;
    print("Google I/O")&lt;br&gt;
case "Facebook":&lt;br&gt;
    print("F8")&lt;br&gt;
case "Microsoft":&lt;br&gt;
    print("Microsoft Build")&lt;br&gt;
default:&lt;br&gt;
    print("Tech Event")&lt;br&gt;
}&lt;br&gt;
`&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here case works like &lt;strong&gt;if&lt;/strong&gt; keyword and title inside the quotation mark work like a condition you saw in the round braces of &lt;strong&gt;if ...else&lt;/strong&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  What is default?
&lt;/h3&gt;

&lt;p&gt;Answer. This is also one point where if ...else condition doesn't match with switch case. A default is required for every switch case to work as &lt;strong&gt;else&lt;/strong&gt;, though not the same but similar. After every condition fails, default comes into action for giving out the same initial value of a declaration of variable/constant or whatever you set.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What's next?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We're happy to help you understand the concept of Conditional Statements, but in programming, there are other important topics that you might have gone through or are remaining. So we encourage you to go over the concept of functions, arrays, or dictionaries. If you are already familiar with all those topics, consider learning structures, closures, or methods next. Till then, &lt;/p&gt;

&lt;p&gt;Eat. Sleep. SwiftAnytime. Repeat.&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Variables in Swift</title>
      <dc:creator>Mayank Gupta👨‍💻🥑</dc:creator>
      <pubDate>Tue, 17 May 2022 16:02:44 +0000</pubDate>
      <link>https://dev.to/iosmayank/variables-in-swift-bo4</link>
      <guid>https://dev.to/iosmayank/variables-in-swift-bo4</guid>
      <description>&lt;p&gt;What are variables? Most of you are here to understand the first and foremost concept of programming, but we will help you answer with the simple steps:&lt;/p&gt;

&lt;p&gt;If you were attentive and loved Maths as your subject in High School, it's easy to identify this concept quickly.&lt;br&gt;
Never mind, forget that Math Class from your boring instructor. We are here to explain it in an easy way. For that, speak out the word variable. Does it sound somewhere close to the word "Vary", well now we assume you know the meaning and would have guessed the definition of the term "variables".&lt;/p&gt;

&lt;p&gt;Variables are one of the concepts in programming and not just Swift. It stores some value whether it may be an Int, Float, String, Double, etc.&lt;/p&gt;

&lt;p&gt;If you are new to Swift or programming, we suggest you go over the upcoming article to understand Data Types (Int, String, Double, etc.)&lt;/p&gt;

&lt;p&gt;Also, we highly encourage you to implement it with us by running the Playgrounds app by Apple. You can download it from the App Store. If you don't have Mac but an iPad, it will run on that too!&lt;/p&gt;

&lt;p&gt;Let us now declare a variable. To declare a variable in Swift, we first write down "var". You can say it's also a short form of the term "Variable". If you see the color of the keyword we entered, it would be the pink one. After that, you write the name of your variable, which you can see in the following examples:&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;
&lt;span class="c1"&gt;//or&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;age&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations! You have declared a variable, but to give/store a value. Follow the last step.&lt;/p&gt;

&lt;p&gt;The last thing we do is give them the value, like&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Krishna"&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the variable "name" is a String, you have to add a quotation mark. Also, we know that constants don't vary, but variables do. So, if you want to change the age to 72, it's easy to do that (not in real life).&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="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;72&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time you don't write that keyword "var" again, write the variable name "age" or whatever you declared, and add the new value to it!&lt;/p&gt;

&lt;h3&gt;
  
  
  What's next?
&lt;/h3&gt;

&lt;p&gt;It's beautiful to start a journey but don't stop here, push yourself to learn other concepts. To understand the following concepts in order, you must be understanding the concept of &lt;a href="https://www.swiftanytime.com/blog/constants-in-swift/" rel="noopener noreferrer"&gt;constants&lt;/a&gt; next. Have a great time ahead!&lt;/p&gt;

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

</description>
      <category>swift</category>
      <category>ios</category>
      <category>mobile</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Arrays in Swift</title>
      <dc:creator>Mayank Gupta👨‍💻🥑</dc:creator>
      <pubDate>Thu, 12 May 2022 10:31:10 +0000</pubDate>
      <link>https://dev.to/iosmayank/arrays-in-swift-4dpf</link>
      <guid>https://dev.to/iosmayank/arrays-in-swift-4dpf</guid>
      <description>&lt;p&gt;Till now, what you saw is assigning only one value to a variable/constant. You might have one question though, what if I have a bunch of values to store?&lt;/p&gt;

&lt;p&gt;Answer to that question is quite straight forward, to store the collection of Hot wheels, we keep it in a rectangle or square box. Just like that, if you want to store a collection of items in one variable, an array helps there. Arrays can be defined as a collection of same type of values in an ordered position. Let's first have a look at the syntax,&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;arraySyntax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, after declaring and naming a variable, you first open a square bracket and then store a value in it. Now, add multiple items by separating them up with a comma, and once you are done adding your values, close it with a closing square bracket.&lt;/p&gt;

&lt;p&gt;The values stored are an Int data type. Swift automatically recognizes and assigns the data type after adding the first value, which means you can't add a String or Double value to your variable or a constant later.&lt;/p&gt;

&lt;p&gt;Let's access/bring an item out of an array, but you must know that a number is allotted for its position whenever you have a new element in your Array. We don't call it a number, but an index and a group are called indices.&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="n"&gt;arraySyntax&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;arraySyntax&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;When changing a specific item of your collection, we can do just like swapping a value of a single variable.&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="n"&gt;arraySyntax&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="n"&gt;arraySyntax&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Almost every programming language out there will start their count from 0, Swift is one of them. So, when you put an index value &lt;strong&gt;1&lt;/strong&gt; when referring to an Array item and getting it printed, you will be surprised by the output. Since the result displayed will be &lt;strong&gt;2&lt;/strong&gt; instead of &lt;strong&gt;1&lt;/strong&gt;, we start with 0 to refer to the first element.&lt;/p&gt;

&lt;p&gt;We discussed earlier in this article that Swift automatically recognizes a data type. Now, here is one thing we would like you to try:&lt;/p&gt;

&lt;p&gt;Copy the code from the snippet and print it,&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;team&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Tim Cook"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Craig Federighi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eddy Cue"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Jony Ive"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Krishna Babani"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Xcode or Playgrounds App won't print anything; instead, it shows an error &lt;strong&gt;Heterogeneous collection literal could only be inferred to&lt;br&gt;
"[Any]'; add explicit type annotation if this is intentional" and a solution "Insert " as [Any]"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Tapping on fix will solve your error and print an Integer as well. One in a million would try storing an item different from the stored ones, and for that, you will need to set your variable type to &lt;strong&gt;Any&lt;/strong&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;team&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Tim Cook"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Craig Federighi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eddy Cue"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Jony Ive"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Krishna Babani"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This also means you can set a data type to String, Int, or Double.&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;team&lt;/span&gt;&lt;span class="p"&gt;:&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="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Tim Cook"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Craig Federighi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eddy Cue"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Jony Ive"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Krishna Babani"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we will need to drop an Integer from our array/collection.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create an Array?
&lt;/h2&gt;

&lt;p&gt;The next important thing to remember is that the square brackets for arrays cannot be left empty. (There is a twist)&lt;/p&gt;

&lt;p&gt;For example,&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;friends&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hence, this results in an error &lt;strong&gt;Empty collection literal requires an explicit type&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To solve your question: "How can we just create an array and store the values later ?"&lt;/p&gt;

&lt;p&gt;The answer is simple, specify what type of data you will store in your Array. There will be a slight change in Syntax:&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;friends&lt;/span&gt;&lt;span class="p"&gt;:&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="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="c1"&gt;// Can now keep your Array empty.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The other notable Syntax which also means the same:&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;friends&lt;/span&gt;&lt;span class="p"&gt;:&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Array Operators
&lt;/h2&gt;

&lt;p&gt;We can merge or combine two arrays but must be a little careful when doing it.&lt;/p&gt;

&lt;p&gt;To combine two arrays, rules of adding an item to your Array will also apply here. This means that both the arrays must hold the same data type, or else you might get the same or similar error that we saw above.&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;teamApple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Craig"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Tim"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Greg"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Eddy"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Jeff"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// List 1&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;teamSA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Krishna"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Mayank"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Krish"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Amit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Raj"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// List 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Assume that Apple hosts an in-person WWDC 2023. Someone at Apple got a task to create two lists and send the invites to people from List 1 and rest later after a week. In this case, the team of Apple executives and our team. &lt;/p&gt;

&lt;p&gt;The demo invites are sent out to the people from List 1. Once they confirm, the guy then sends out an invite to us (SA Team).&lt;/p&gt;

&lt;p&gt;But now, if he/she wants to know who is coming to this event, he will be combining these lists into a single list.&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;membersAttending&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;teamApple&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;teamSA&lt;/span&gt;
&lt;span class="c1"&gt;// You won't see an error as both the variables hold the same type of data.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all for the concept of Arrays. Keep yourself engaged in this learning process because Determination and Consistency are the only keys to success. We encourage you to learn more related topics next. Till then,&lt;/p&gt;

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

</description>
      <category>swift</category>
      <category>ios</category>
      <category>mobile</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Colors and Gradient in SwiftUI</title>
      <dc:creator>Mayank Gupta👨‍💻🥑</dc:creator>
      <pubDate>Wed, 11 May 2022 11:04:56 +0000</pubDate>
      <link>https://dev.to/iosmayank/colors-and-gradient-in-swiftui-d7e</link>
      <guid>https://dev.to/iosmayank/colors-and-gradient-in-swiftui-d7e</guid>
      <description>&lt;p&gt;Colors are powerful. They can convey a message by the visuals and can give a certain kind of vibe to the art (for us, the app). It is one of those inevitable things that you are going to encounter while making pixel perfect production app. So, without further ado, let's dive into colors in SwiftUI. &lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;The primary usage that most developers encounter first in SwiftUI App Development is as for adding background and color to the view. Let's look into an example:&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="kt"&gt;VStack&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce at ante sit amet turpis pretium porta. Cras id dui vel orci scelerisque efficitur et quis dui. Suspendisse tortor nisi, euismod scelerisque sollicitudin in, euismod non ligula. Nulla id risus lobortis nunc venenatis tincidunt. Vestibulum elementum ex est, non sagittis odio dictum eget. Suspendisse non dolor pulvinar, vulputate ligula sit amet, aliquam lorem. Fusce leo ipsum, tempus quis ipsum a, accumsan sagittis justo. Duis placerat, purus vitae ornare semper, enim eros egestas metus, eu euismod arcu ante sed dui. Proin blandit auctor fermentum. Nulla elementum nibh sit amet metus suscipit, eget cursus sapien eleifend. Curabitur tempus mi malesuada quam tempus posuere. Pellentesque ullamcorper tempus dapibus."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;foregroundColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;white&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;background&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Color&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;p&gt;In the above code, you have &lt;a href="https://www.swiftanytime.com/blog/text-in-swiftui" rel="noopener noreferrer"&gt;text&lt;/a&gt; with a lorem ipsum paragraph in a VStack. Then, &lt;code&gt;padding()&lt;/code&gt; modifier is added to add some spacing. Then, &lt;code&gt;foregroundColor&lt;/code&gt; modifier is used to give white foreground text to it. Moreover, &lt;code&gt;background&lt;/code&gt; modifier is also added to the view to give the particular color as background. &lt;/p&gt;

&lt;p&gt;Note here, you can also use color as a view.&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;ContentView&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;some&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;Color&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;red&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;Result: &lt;/p&gt;

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

&lt;p&gt;In the above code, you can observe how the &lt;code&gt;Color.red&lt;/code&gt; acts as a standalone view and takes all the available space. &lt;/p&gt;

&lt;h2&gt;
  
  
  Primary And Secondary Colors
&lt;/h2&gt;

&lt;p&gt;You can access a few set of colors in SwiftUI but there is a couple of colors that we are going discuss today : Primary and secondary colors. Before understanding it, let's look at a code example:&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="kt"&gt;VStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;alignment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;leading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;spacing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

            &lt;span class="kt"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Swift Anytime"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bold&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;font&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;foregroundColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;primary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="kt"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Think of Swift. Think of Us."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;foregroundColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;secondary&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;Result: &lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;Note: Color Scheme refers system wide color appearance in apple ecosystem. ie. light mode and dark mode. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the above code, you have a VStack with a couple of text: one with primary color as foreground color and second one as secondary color as foreground color. When the system mode is switched, these colors change too. It can be used for coloring the text content so that it's color responds to the system mode change in a well manner. &lt;/p&gt;

&lt;h2&gt;
  
  
  Initializing Colors
&lt;/h2&gt;

&lt;p&gt;SwiftUI provides with multiple ways to initialize colors. One way to do it is using &lt;code&gt;Color(red: ..., green: ..., blue: ...)&lt;/code&gt;. You can provide the RGB (Red, Green, blue) value on a 0-1 range.&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="kt"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;red&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="sr"&gt;/255, green: 105/255, blue: 180/255) //&lt;/span&gt; &lt;span class="kt"&gt;Hot&lt;/span&gt; &lt;span class="kt"&gt;Pink&lt;/span&gt; &lt;span class="kt"&gt;Color&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also initialize color with UIColor (from UIKit) and NSColor (from AppKit). Let's try to initialize hot pink with UIColor this time.&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="kt"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;uiColor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;red&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="sr"&gt;/255, green: 105/255, blue: 180/&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;alpha&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result of both the initialization given above would be the following: &lt;/p&gt;

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

&lt;h2&gt;
  
  
  Using Color from Assets folder
&lt;/h2&gt;

&lt;p&gt;You can also add color via Assets folder. For adding a color set, you go to the Assets Folder -&amp;gt; Click "+" button on bottom left corner -&amp;gt; "Add Color Set". &lt;/p&gt;

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

&lt;p&gt;You can customize the color for a particular color scheme or can have the same color for both modes.&lt;/p&gt;

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

&lt;p&gt;The asset colors can be used in app by &lt;code&gt;Color("colorName")&lt;/code&gt;. In our example, this color can be used in code by:&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="kt"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SomeColor"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Opacity
&lt;/h2&gt;

&lt;p&gt;You can play around with the opacity of the color by using &lt;code&gt;opacity(...)&lt;/code&gt; modifier where you can enter the opacity between the range of 0 to 1.&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="kt"&gt;ZStack&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kt"&gt;Color&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gray&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Setting the color opacity to 0.5 &lt;/span&gt;
                &lt;span class="kt"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Swift Anytime"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;foregroundColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;black&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;font&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;largeTitle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bold&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;Result: &lt;/p&gt;

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

&lt;h2&gt;
  
  
  Gradients
&lt;/h2&gt;

&lt;p&gt;SwiftUI provides three type of gradients. Gradients can be used for adding vibrant and bright backgrounds. Let's explore those.&lt;/p&gt;

&lt;h3&gt;
  
  
  Linear Gradient
&lt;/h3&gt;

&lt;p&gt;The simpliest gradient is linear gradient. You can provide the start and end point along with an array of colors.&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="kt"&gt;LinearGradient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;orange&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;red&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
                       &lt;span class="nv"&gt;startPoint&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                       &lt;span class="nv"&gt;endPoint&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;p&gt;In the above example, the gradient starts and end at the top and center respectively. Moreover, you can observe that using a correct combination of colors can create a great blend of colors with a very organic gradient.&lt;/p&gt;

&lt;h3&gt;
  
  
  Angular Gradients
&lt;/h3&gt;

&lt;p&gt;Angular Gradients (also called "conic" gradients) are the gradients where the color change is based on the angle.&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="kt"&gt;AngularGradient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;red&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;green&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;purple&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pink&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
                           &lt;span class="nv"&gt;center&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                           &lt;span class="nv"&gt;startAngle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;degrees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                           &lt;span class="nv"&gt;endAngle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;degrees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;360&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;p&gt;In the above code, the angular gradient is initialized with an array of colors, the center of the gradient and the start and end angle. Note here that the start and end angle is added in form of degrees here. But, you can also add it in form of radians by &lt;code&gt;.radians(...)&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Radial Gradients
&lt;/h2&gt;

&lt;p&gt;Radial Gradient basically starts adding up the color concentrically from the center point and scales to fit it in the given radius ie. the start and end radius&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="kt"&gt;RadialGradient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;red&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;yellow&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;green&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
                       &lt;span class="nv"&gt;center&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;center&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                       &lt;span class="nv"&gt;startRadius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                       &lt;span class="nv"&gt;endRadius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;p&gt;You can observe how the colors start to eccentrically get around each other, starting from red and ending it at green, thus, forming a radial gradient.&lt;/p&gt;

&lt;p&gt;Congratulations! You deserve to celebrate this moment. Today you learned about colors and its different initilization, setting its opacity and its usage along with different type of gradients. We encourage you to read more related articles till then, have a great time ahead.&lt;/p&gt;

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

</description>
      <category>swiftui</category>
      <category>swift</category>
      <category>ios</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
