<?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: Fernando Martín Ortiz</title>
    <description>The latest articles on DEV Community by Fernando Martín Ortiz (@fmo91).</description>
    <link>https://dev.to/fmo91</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%2F232060%2Fa682a973-7ac0-4009-a85d-4bbe818a0f8f.jpg</url>
      <title>DEV Community: Fernando Martín Ortiz</title>
      <link>https://dev.to/fmo91</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fmo91"/>
    <language>en</language>
    <item>
      <title>This week I learned #1</title>
      <dc:creator>Fernando Martín Ortiz</dc:creator>
      <pubDate>Sun, 07 Jan 2024 21:09:41 +0000</pubDate>
      <link>https://dev.to/fmo91/this-week-i-learned-1-1jli</link>
      <guid>https://dev.to/fmo91/this-week-i-learned-1-1jli</guid>
      <description>&lt;p&gt;&lt;strong&gt;On these posts, I will list what I learned, or discovered, or reflected about during the last week. I believe that, by doing this, I will learn much better and keep learnings at a good pace. Also, as a byproduct, probably some readers will also learn something from these, or maybe they think I’m wrong and would like to comment something, so we can debate.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In any case, welcome every one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Life/Work balance&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Working from home is amazing, and it’s much more amazing when you can share time with your partner and child. Seeing my baby grow, and being able to spend more time with him is much better than any benefit I could get.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Python&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Virtual environments are a mess. It’s much better to use Docker. There is an extension on Visual Studio Code called “Dev Containers”. It’s developed by Microsoft, and lets you run a terminal on a Docker Container, with the content of your VS Code project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Elixir&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GenServer is the default way of handling state in Elixir. A GenServer stands for “Generic Server”, and it’s a process which run an contains a piece of state. You can send messages to that GenServer process by using its process ID (or pid), and mutate that state in a very “functional” way.&lt;/li&gt;
&lt;li&gt;A module you own should &lt;code&gt;use GenServer&lt;/code&gt; to be usable as a GenServer. Think of it as an interface being implemented.&lt;/li&gt;
&lt;li&gt;If you include your GenServer as an application &lt;code&gt;children&lt;/code&gt;, then the module you send to it is started as a process right away when the app starts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Web Development&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Microsoft Azure has a service called Azure Container Apps, in which every app is containerized using Docker. All the apps (you can think of them as microservices) communicate using a Virtual Network (v-net).&lt;/li&gt;
&lt;li&gt;Microsoft Azure has a language (DSL) called Bicep, which is similar to AWS CloudFoundation, to maintain infrastructure using IaC.&lt;/li&gt;
&lt;li&gt;DAPR (Distributed Apps Runtime) is an initiative that was started by Microsoft, and nowadays maintained by the Cloud Native Foundation. Using DAPR, you can use the services defined on the standard to communicate with other microservices, handle state (using Redis, for example), add Telemetry, use Pub/Sub mechanisms, and other things. The specs defined in DAPR are like “abstract classes”. There is always an actual “component” which implements the spec. For example, you use the DAPR state management service, but under the hood, you’re using Redis to keep track of key/value pairs for that state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Artificial Intelligence&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This week, I’ve been reading content and preparing myself for an upcoming project that will use LLMs.&lt;/li&gt;
&lt;li&gt;This article is amazing: &lt;strong&gt;&lt;a href="https://github.blog/2023-10-30-the-architecture-of-todays-llm-applications/"&gt;The architecture of today's LLM applications&lt;/a&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;IoT&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I’m preparing myself to work on internet of things projects using Nerves.&lt;/li&gt;
&lt;li&gt;Nerves is a tool written in Elixir for creating IoT applications.&lt;/li&gt;
&lt;li&gt;You can actually run Nerves project without having an actual device (like a Raspberry pi)! This is called “Host Mode”.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>What’s the model?</title>
      <dc:creator>Fernando Martín Ortiz</dc:creator>
      <pubDate>Fri, 05 Nov 2021 01:28:22 +0000</pubDate>
      <link>https://dev.to/fmo91/whats-the-model-4nok</link>
      <guid>https://dev.to/fmo91/whats-the-model-4nok</guid>
      <description>&lt;p&gt;I teach iOS development as a side job. I work 9-5 (well, actually, it’s a bit earlier than that, since I’m working for an English company at this moment). After I finish working, three times a week, I teach iOS development to different people, for two hours. They are starting their careers, and they are developing their intuition about software engineering.&lt;/p&gt;

&lt;p&gt;That’s really interesting from the point of view of somebody who has been in the industry for some years now. It’s good to remember how you started and what errors did you make at the beginning. &lt;/p&gt;

&lt;p&gt;Today, I’m going to talk to you about an error I see over and over again, and recently, I think I reached a way of making people think about it, and reason about it.&lt;/p&gt;

&lt;p&gt;Imagine you are developing an app for ordering food from restaurants: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have a listwith different dishes and options. &lt;/li&gt;
&lt;li&gt;You can add more units of any of the options you have in the list.&lt;/li&gt;
&lt;li&gt;Each item in the list has the number of items of that type you’ve already added, it will let you add or sub any number of items at any moment.&lt;/li&gt;
&lt;li&gt;Whenever you add food, there is a shopping cart or something like that at the bottom of the screen where you can see immediately updated the price of the food you’re buying.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fairly simple, straightforward problem.&lt;/p&gt;

&lt;p&gt;So, here is the thing. There are many ways on how to develop something like that. Imagine you’re using UIKit for now, and not SwiftUI. You would have tableView or collectionView cells for the items in the list, and you would sum to the amount that you have in the bottom.&lt;/p&gt;

&lt;p&gt;This is how people tend to design these kind of apps when they start:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They detect the event to add more items of a certain type. &lt;/li&gt;
&lt;li&gt;They check the label in the cell to know how many items of that type they have already added.&lt;/li&gt;
&lt;li&gt;They add one to that number.&lt;/li&gt;
&lt;li&gt;They update the label in the cell.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And they have similar solutions for the different parts of the UI in this and similar problems. &lt;strong&gt;They rely on the UI as the source of truth.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So I started to ask them: &lt;strong&gt;What is the model?&lt;/strong&gt;. I start any exercise with them in the same way. Regardless of how simple of complex a screen is, I start asking them: What is the model in this UI?&lt;/p&gt;

&lt;p&gt;The other thing that’s related to this I use to tell them from the very beginning is: We are UI developers. As UI developers, our goal isn’t only to deliver delightful UIs with animations and beatiful graphics. &lt;strong&gt;Our goal is to ensure consistency between the model and the UI.&lt;/strong&gt; The part about the UI is usually easily understandable. The part about the model is very hard to understand when you’re starting.&lt;/p&gt;

&lt;p&gt;Our goal is to ensure consistency between the model and the UI. You have a UI, what is the model?&lt;/p&gt;

</description>
      <category>ios</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Navigation in iOS apps</title>
      <dc:creator>Fernando Martín Ortiz</dc:creator>
      <pubDate>Mon, 03 May 2021 22:13:37 +0000</pubDate>
      <link>https://dev.to/fmo91/navigation-in-ios-apps-4jm3</link>
      <guid>https://dev.to/fmo91/navigation-in-ios-apps-4jm3</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This article is part of the course&lt;/em&gt; &lt;strong&gt;Introduction to iOS using UIKit&lt;/strong&gt; &lt;em&gt;I've given many times in the past. The course was originally in Spanish, and I decided to release it in English so more people can read it and hopefully it will help them.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;So far, we've seen example applications where we only had a single view in the screen. In real world apps we have several screens per app. The user starts their journey in one of them but depending on the actions they take, the app will show them other screens.&lt;br&gt;
&lt;strong&gt;The ability of moving between screens in an app is called navigation.&lt;/strong&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Stacks
&lt;/h1&gt;

&lt;p&gt;What is a stack?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A stack is a data structure in which we can insert elements.&lt;/li&gt;
&lt;li&gt;Whenever we add an element to the stack (&lt;strong&gt;push&lt;/strong&gt;), it will be put at the top.&lt;/li&gt;
&lt;li&gt;If we remove an element from the stack (&lt;strong&gt;pop&lt;/strong&gt;), we'll always remove the top element in the stack, so the element that was below the top, will become the top after that.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4d7ojjooyxzmxmakp789.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4d7ojjooyxzmxmakp789.png" alt="stack-01"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Navigation Stack
&lt;/h1&gt;

&lt;p&gt;In iOS, navigation works using one or many stacks. &lt;strong&gt;Each element in the navigation stack is a screen&lt;/strong&gt;, and each screen is represented by a &lt;code&gt;UIViewController&lt;/code&gt;.&lt;br&gt;
The element (screen) that is in the top of the navigation stack is the screen that's visible to the user in the app.&lt;/p&gt;
&lt;h1&gt;
  
  
  Present/Dismiss
&lt;/h1&gt;

&lt;p&gt;There is a "global" navigation stack in iOS. We don't have to do anything to create it. Just by creating an app in iOS, we'll have that global navigation stack ready to be used.&lt;br&gt;
Every &lt;code&gt;UIViewController&lt;/code&gt; subclass have a &lt;code&gt;present&lt;/code&gt; and a &lt;code&gt;dismiss&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;Let's see an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyia6dwzqmp3tl1z9ut2t.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyia6dwzqmp3tl1z9ut2t.png" alt="image"&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkcpy4qpqmlbcip6lfcjk.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkcpy4qpqmlbcip6lfcjk.png" alt="image"&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0asufpp8z9oh7bksi8ez.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0asufpp8z9oh7bksi8ez.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we want to navigate from &lt;code&gt;FirstScreenViewController&lt;/code&gt; to &lt;code&gt;SecondScreenViewController&lt;/code&gt;, we need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Instantiate &lt;code&gt;SecondScreenViewController&lt;/code&gt; from a method on &lt;code&gt;FirstScreenViewController&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Call the &lt;code&gt;present&lt;/code&gt; method right after that, passing to it the &lt;code&gt;SecondScreenViewController&lt;/code&gt; instance, a &lt;code&gt;Bool&lt;/code&gt; indicating whether we want to present the screen using an animation transition, and optionally, also a closure that will be executed as soon as the transition has finished.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;FirstScreenViewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIViewController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;@IBAction&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;navigateButtonPressed&lt;/span&gt;&lt;span class="p"&gt;()&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;secondViewController&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;SecondViewController&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;present&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secondViewController&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&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;p&gt;&lt;a href="https://drive.google.com/file/d/1lL91lSqua-0LNz_e7dPCS7RbmzHj6SKU/view?usp=sharing" rel="noopener noreferrer"&gt;Video&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Optionally, we can configure the property &lt;code&gt;modalPresentationStyle&lt;/code&gt; in &lt;code&gt;SecondScreenViewController&lt;/code&gt; with the value &lt;code&gt;UIModelPresentationStyle.fullScreen&lt;/code&gt; before navigating to it, so the destination view controller will fit the full size of the screen.&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;@IBAction&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;navigateButtonPressed&lt;/span&gt;&lt;span class="p"&gt;()&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;secondViewController&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;SecondViewController&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;secondViewController&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;modalPresentationStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIModalPresentationStyle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fullScreen&lt;/span&gt;
    &lt;span class="nf"&gt;present&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secondViewController&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&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;a href="https://drive.google.com/file/d/1YQjW8BAcZUkD6EAuFJe02G8B6BCvUdz_/view?usp=sharing" rel="noopener noreferrer"&gt;Video&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The problem we have now is that, as it's now fullscreen, we can't go back to the first screen anymore. To avoid this, we can configure an action in the second screen, using the &lt;code&gt;dismiss&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;dismiss&lt;/code&gt; method takes two parameters: a &lt;code&gt;Bool&lt;/code&gt; indicating  if we want an animated transition, and an &lt;code&gt;optional closure&lt;/code&gt;, in case we want to execute custom code after the transition has finished.&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;@IBAction&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;goBackButtonScreen&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;dismiss&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&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;a href="https://drive.google.com/file/d/1ChJDxUY7h0xzGCm-Zf3SRW9aP5vU5qj7/view?usp=sharing" rel="noopener noreferrer"&gt;Video&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  UINavigationController
&lt;/h1&gt;

&lt;p&gt;However, the practice of doing present/dismiss is commonly used to show a modal to the user in specific situatioons.&lt;/p&gt;

&lt;p&gt;Most commonly, we'll use &lt;code&gt;UINavigationController&lt;/code&gt;, which is a class that contains an internal navigation stack.&lt;/p&gt;

&lt;p&gt;To add a screen/controller to a &lt;code&gt;UINavigationController&lt;/code&gt;, we'll use the &lt;code&gt;push&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;To remove a screen/controller from a &lt;code&gt;UINavigationController&lt;/code&gt;, we'll use the &lt;code&gt;pop&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;UINavigationController&lt;/code&gt; is instantiated with a &lt;code&gt;UIViewController&lt;/code&gt; that's called the &lt;strong&gt;root&lt;/strong&gt; view controller, and that will be the bottom of the stack.&lt;/p&gt;

&lt;p&gt;In addition, using a &lt;code&gt;UINavigationController&lt;/code&gt; gives us a view at the top of our screens that's called &lt;code&gt;UINavigationBar&lt;/code&gt;, that contains the current screen title, helper buttons at the sides, and back button, among others.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F88slqe1b2ew6m7lhl1ax.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F88slqe1b2ew6m7lhl1ax.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's try to modify the second screen of our app, so that it will be contained inside a &lt;code&gt;UINavigationController&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Remember this action:&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;@IBAction&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;navigateButtonPressed&lt;/span&gt;&lt;span class="p"&gt;()&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;secondViewController&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;SecondViewController&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;secondViewController&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;modalPresentationStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIModalPresentationStyle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fullScreen&lt;/span&gt;
    &lt;span class="nf"&gt;present&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secondViewController&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&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;We'll modify it so that &lt;code&gt;secondViewController&lt;/code&gt; will be inside a &lt;code&gt;UINavigationController&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;@IBAction&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;navigateButtonPressed&lt;/span&gt;&lt;span class="p"&gt;()&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;secondViewController&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UINavigationController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;rootViewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SecondViewController&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="n"&gt;secondViewController&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;modalPresentationStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UIModalPresentationStyle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fullScreen&lt;/span&gt;
    &lt;span class="nf"&gt;present&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secondViewController&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&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;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffayx8ytraxevvxcdbjnf.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffayx8ytraxevvxcdbjnf.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The view that we see at the top is the &lt;code&gt;UINavigationBar&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Something we can do to personalize it, is to assign a title to it. To do so, we'll modify the property &lt;code&gt;title&lt;/code&gt; in the view controller:&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;class&lt;/span&gt; &lt;span class="kt"&gt;SecondViewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIViewController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Second"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;@IBAction&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;goBackButtonPressed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;dismiss&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&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;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F81heud6k5g4oeuxc2cbj.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F81heud6k5g4oeuxc2cbj.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to perform a transition in a &lt;code&gt;UINavigationController&lt;/code&gt; we need to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create another screen. I mean, a new &lt;code&gt;UIViewController&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Inside &lt;code&gt;SecondScreenViewController&lt;/code&gt;, perform a &lt;code&gt;push&lt;/code&gt; to the third screen.&lt;/li&gt;
&lt;li&gt;To do this &lt;code&gt;push&lt;/code&gt;, let's keep in mind that the class &lt;code&gt;UIViewController&lt;/code&gt; has an optional property called &lt;code&gt;navigationController&lt;/code&gt; with type &lt;code&gt;UINavigationController?&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa8ae4m80a762vcrfndun.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa8ae4m80a762vcrfndun.png" alt="image"&gt;&lt;/a&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;@IBAction&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;thirdScreenButtonPressed&lt;/span&gt;&lt;span class="p"&gt;()&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;thirdViewController&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;ThirdScreenViewController&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;navigationController&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pushViewController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thirdViewController&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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;a href="https://drive.google.com/file/d/1w7CfXtXx17AxPp_Pnq1erR0RfCvp8E-8/view?usp=sharing" rel="noopener noreferrer"&gt;Video&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You might have noticed that the navigation bar automatically adds a button to return to the previous screen. If we want to do a back navigation programmatically, we'll use the &lt;code&gt;pop&lt;/code&gt; method from &lt;code&gt;UINavigationController&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj578zxxnk2kz3073gd3k.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj578zxxnk2kz3073gd3k.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Small comment: to add a padding to the button, we can set the content insets of it in the attributes inspector&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ojixtzl00ym8inv4711.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ojixtzl00ym8inv4711.png" alt="image"&gt;&lt;/a&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;@IBAction&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;popButtonPressed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;navigationController&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popViewController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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;a href="https://drive.google.com/file/d/1QfkGaCdhGdd_3k6eJmHfJLq7u0PzVfj2/view?usp=sharing" rel="noopener noreferrer"&gt;Video&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Passing data
&lt;/h1&gt;

&lt;p&gt;How could we send data from one screen to the next one. The answer is, that before performing the navigation, we can set a variable in the destination &lt;code&gt;UIViewController&lt;/code&gt;. &lt;br&gt;
We must &lt;strong&gt;NEVER&lt;/strong&gt; configure a view on the destination controller, because they aren't initialized until the navigation is committed.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Using Xibs</title>
      <dc:creator>Fernando Martín Ortiz</dc:creator>
      <pubDate>Mon, 03 May 2021 21:20:22 +0000</pubDate>
      <link>https://dev.to/fmo91/using-xibs-2ime</link>
      <guid>https://dev.to/fmo91/using-xibs-2ime</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This article is part of the course&lt;/em&gt; &lt;strong&gt;Introduction to iOS using UIKit&lt;/strong&gt; &lt;em&gt;I've given many times in the past. The course was originally in Spanish, and I decided to release it in English so more people can read it and hopefully it will help them.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  What is a Storyboard?
&lt;/h1&gt;

&lt;p&gt;A Storyboard is one way of using Interface Builder for designing and developing views in iOS. Each Storyboard described one or many screens. Each screen in a Storyboard is linked to a &lt;code&gt;UIViewController&lt;/code&gt;.&lt;br&gt;
We can navigate between the screens that are designed using Storyboards using &lt;strong&gt;Segues&lt;/strong&gt;.&lt;br&gt;
In theory, it isn't required for us to instantiate view controllers manually if we decide to use Storyboards, because the navigation between them and the data that we need to pass between a screen to the next one, is all handled using segues.&lt;br&gt;
In practice, this pattern can result not as intuitive as we want.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is a XIB?
&lt;/h1&gt;

&lt;p&gt;A Xib is another way of using Interface Builder for designing and developing view in iOS. &lt;strong&gt;Each Xib describes a single screen.&lt;/strong&gt; As in Storyboards, each screen in a Xib is linked to a &lt;code&gt;UIViewController&lt;/code&gt;.&lt;br&gt;
Navigation between screens are defined outside of the Xib file, by manually instantiating controllers using their &lt;code&gt;init&lt;/code&gt; method, as we've seen during &lt;a href="https://dev.to/fmo91/introduction-to-swift-part-2-7m0"&gt;the second lesson&lt;/a&gt; about Swift.&lt;/p&gt;

&lt;h1&gt;
  
  
  Which one are we going to use?
&lt;/h1&gt;

&lt;p&gt;During this course, we'll use Xibs, because of a couple of reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It's the most widely used way to develop views in the industry right now.&lt;/li&gt;
&lt;li&gt;A Xib file loads quicker than a Storyboard in Xcode, because Xib files are much smaller. In a bigger project, with bigger Storyboards, this is even more important.&lt;/li&gt;
&lt;li&gt;A Xib is commonly placed alongside its Swift counterpart. In a Storyboard, if we're looking for a specific view, this is slightly more inconvenient.&lt;/li&gt;
&lt;li&gt;Instantiating a controller in code is more intuitive and it's more consistent with other patterns in iOS.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Base App
&lt;/h1&gt;

&lt;p&gt;When we create a project in Xcode, it's created using Storyboards. Adapting it to use Xibs instead of Storyboards is a boring task that doesn't teach us much.&lt;br&gt;
In order to avoid doing that, you can download &lt;a href="https://github.com/fmo91/TutorialApp"&gt;this template from my Github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; When you've downloaded it, open &lt;code&gt;TutorialApp.xcworkspace&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In order to create a screen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a folder (group), by right-clicking the group Application and selecting the option &lt;strong&gt;New Group&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Inside the new group, right-click and choose &lt;strong&gt;New File...&lt;/strong&gt; &lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Choose &lt;strong&gt;iOS &amp;gt; Cocoa Touch Class&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MnlwEl_H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06dc7y2pf35re1vq0lne.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MnlwEl_H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06dc7y2pf35re1vq0lne.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enter a name for the new screen, taking care that it's finished with &lt;em&gt;ViewController&lt;/em&gt;, and that's a &lt;strong&gt;Subclass of: UIViewController&lt;/strong&gt;, and also that the option &lt;em&gt;Also create XIB file&lt;/em&gt; is checked (and that the language is Swift).&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Save it in the default location. &lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;And that's it. We've created our first screen using Xibs instead of Storyboards. In the next tutorials, we'll continue doing this process to create other screens and navigating between them. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Introduction to Swift - Part 2</title>
      <dc:creator>Fernando Martín Ortiz</dc:creator>
      <pubDate>Mon, 03 May 2021 00:12:29 +0000</pubDate>
      <link>https://dev.to/fmo91/introduction-to-swift-part-2-7m0</link>
      <guid>https://dev.to/fmo91/introduction-to-swift-part-2-7m0</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This article is part of the course&lt;/em&gt; &lt;strong&gt;Introduction to iOS using UIKit&lt;/strong&gt; &lt;em&gt;I've given many times in the past. The course was originally in Spanish, and I decided to release it in English so more people can read it and hopefully it will help them.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Object oriented programming
&lt;/h1&gt;

&lt;p&gt;In every program we've got two well defined concepts: Data and behavior.&lt;br&gt;
A tuple with five fields that define an address is data. &lt;br&gt;
A function that takes that tuple and returns a &lt;code&gt;String&lt;/code&gt; with its description is behavior.&lt;/p&gt;

&lt;p&gt;Everything we've done so far in the first part of Swift introduction has been defining data on one side and the behavior that acts on that data in another, separated side, which has worked well for us so far. However, the most frequent way of organizing code in modern languages, such as Swift, is integrating data and behavior in cohesive structures such as the ones we'll see today, when we'll review &lt;strong&gt;Object oriented programming&lt;/strong&gt; (OOP).&lt;/p&gt;

&lt;p&gt;This will bring us some advantages. Especially, the ability to abstract. A class will have behavior that can be used from the outside. It means, we'll know &lt;strong&gt;what&lt;/strong&gt; that class can do. However, the &lt;strong&gt;how&lt;/strong&gt; is private to that class. That's known as &lt;strong&gt;encapsulation&lt;/strong&gt;.&lt;/p&gt;
&lt;h1&gt;
  
  
  Class
&lt;/h1&gt;

&lt;p&gt;A class is one of those structures that integrate data and behavior as a unit. Data inside a class is called &lt;strong&gt;attributes&lt;/strong&gt;. Behavior inside a class is modeled as a set of functions known as &lt;strong&gt;methods&lt;/strong&gt;.&lt;br&gt;
In Swift, a class is defined using the keyword &lt;code&gt;class&lt;/code&gt; followed by its name. Attributes will be variables, and methods will be functions.&lt;br&gt;
Let's see an example of a class hierarchy and then we'll explain in detail what just has happened:&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;class&lt;/span&gt; &lt;span class="kt"&gt;Animal&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;name&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;var&lt;/span&gt; &lt;span class="nv"&gt;walks&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;animalType&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="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&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;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;walks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;emitSound&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;walk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="nv"&gt;place&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;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;walks&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;place&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="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;animalType&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="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Dog"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;emitSound&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="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;: Woof!"&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="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;animalType&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="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Cat"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;emitSound&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="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;: Meow!"&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="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;pets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&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="nv"&gt;pets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pets&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pets&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;getHome&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="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;: gets home"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;pet&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;pets&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;pet&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emitSound&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;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;romina&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Romina"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;pets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="kt"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Fausto"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="kt"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Lupi"&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="n"&gt;romina&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getHome&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will print:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Romina: gets home
Fausto: Meow!
Lupi: Woof!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Inheritance
&lt;/h2&gt;

&lt;p&gt;Ok, what just has happened here? First, we've defined a base class, called &lt;code&gt;Animal&lt;/code&gt;. It's called base class because it's the class that will define the base of the class hierarchy, it will define the common set of data and behavior for the other classes in the hierarchy. &lt;/p&gt;

&lt;p&gt;What can an animal do? (behavior). In this case, it can go for a walk, and it can emite a sound. What sound? We don't know that. The sound an animal emits will be define in its subclasses. That's called &lt;strong&gt;inheritance&lt;/strong&gt;. An animal can either be a &lt;code&gt;Cat&lt;/code&gt; or a &lt;code&gt;Dog&lt;/code&gt;, in this example. So that, a &lt;code&gt;Cat&lt;/code&gt; can say &lt;code&gt;Meow&lt;/code&gt;, and define its type as &lt;code&gt;"Cat"&lt;/code&gt;, but it &lt;strong&gt;inherits&lt;/strong&gt; all the behavior defined in its base class.&lt;/p&gt;

&lt;p&gt;What do we know of an animal? (data). In this case, its name and the walks it has taken.&lt;/p&gt;

&lt;p&gt;And &lt;code&gt;animalType&lt;/code&gt;? It's a computed variable, or &lt;strong&gt;getter&lt;/strong&gt;. A getter is essentially a function that returns a value. From the outside of a class, it's seen and it's used a variable that we can't change its value. It takes part of the behavior of a class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encapsulation
&lt;/h2&gt;

&lt;p&gt;A class only exposes part of its data and behavior that we can use from its outside. What is internal to that class is defined with the keyword &lt;code&gt;private&lt;/code&gt;. What is &lt;code&gt;private&lt;/code&gt; can only be accessed from inside that class. What isn't marked as &lt;code&gt;private&lt;/code&gt; is accessible from anywhere else in the app and it's known as the &lt;strong&gt;public interface&lt;/strong&gt; for that class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Init
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;init&lt;/code&gt; method is an especial method of a class, that has the responsibility of define how an object of that class will be created. In this example, the &lt;code&gt;Person&lt;/code&gt; class will have an &lt;code&gt;init&lt;/code&gt; defined like this:&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="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&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="nv"&gt;pets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
     &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pets&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pets&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;self&lt;/code&gt; in this case refers to the same object on which we're working.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;init&lt;/code&gt; doesn't have to be written in order to be used. Consider this 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;let&lt;/span&gt; &lt;span class="nv"&gt;romina&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Persona&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
     &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Romina"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="nv"&gt;pets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
         &lt;span class="kt"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Fausto"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
         &lt;span class="kt"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Lupi"&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;h2&gt;
  
  
  Polymorphism
&lt;/h2&gt;

&lt;p&gt;Functionality defined in a base class can be redefined with the keyword &lt;code&gt;override&lt;/code&gt; in the subclasses. So for instance, &lt;code&gt;emitSound&lt;/code&gt; doesn't have an actual functionality in the base class. But it does in its subclasses. &lt;br&gt;
Subclasses "override" behavior from the base class. For instance:&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;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;emitSound&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="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;: Meow!"&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;What's interesting is that we can refer to dog and cats as &lt;code&gt;Animal&lt;/code&gt; in the &lt;code&gt;pets&lt;/code&gt; &lt;code&gt;Array&lt;/code&gt;. Whenever we call &lt;code&gt;pet.emitSound()&lt;/code&gt; we aren't sure if the pet is a cat or a dog, that's determined in runtime, where each object will execute the behavior defined by its specific subclass.&lt;/p&gt;

&lt;h1&gt;
  
  
  Protocol
&lt;/h1&gt;

&lt;p&gt;A &lt;code&gt;class&lt;/code&gt; answers the question &lt;strong&gt;What is this?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;What is this? A Cat&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is this? A Dog&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is this? A Person&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A &lt;code&gt;protocol&lt;/code&gt;, on the other hand, answers the question &lt;strong&gt;What can this do?&lt;/strong&gt;. &lt;br&gt;
On its simplest form, a protocol (also called &lt;strong&gt;interface&lt;/strong&gt; in other programming languages such as Java), is a set of method signatures under a name. Any class can &lt;strong&gt;implement&lt;/strong&gt; a protocol. If a class implements a protocol, then it must implement all the methods defined on it. Otherwise, the code won't compile.&lt;/p&gt;

&lt;p&gt;Let's see 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="kd"&gt;protocol&lt;/span&gt; &lt;span class="kt"&gt;EmitsSound&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;emitSound&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Animal&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;EmitsSound&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;emitSound&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="s"&gt;"Woof!"&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="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Animal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;EmitsSound&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;emitSound&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="s"&gt;"Meow!"&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="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Ringer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;EmitsSound&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;emitSound&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="s"&gt;"Ring!"&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;p&gt;Note here that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A class can only inherit from a single class. However, it can implement as many protocols as we need. If a class inherits from another class and also implements protocols, what is written at the right of &lt;code&gt;:&lt;/code&gt; must be first the base class, and then the protocols to be implemented.&lt;/li&gt;
&lt;li&gt;A Dog, a Cat, and a Ringer are now of the same type. All of them are of the type &lt;code&gt;EmitsSound&lt;/code&gt;. And as such, we can use them in an &lt;code&gt;Array&lt;/code&gt;, for instance:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;SoundEffects&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;sounds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;EmitsSound&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;sounds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;EmitsSound&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sounds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sounds&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;play&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;sound&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sounds&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;sound&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emitSound&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;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;getHomeSounds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;SoundEffects&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;sounds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Ringer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="kt"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="kt"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;()])&lt;/span&gt;
&lt;span class="n"&gt;getHomeSounds&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;play&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// Ring!&lt;/span&gt;
&lt;span class="c1"&gt;// Meow!&lt;/span&gt;
&lt;span class="c1"&gt;// Woof!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we've implemented &lt;code&gt;EmitsSound&lt;/code&gt; for all our objects, regardless of their class, we can now use them to emit sounds.  In this example, we use this to implement sound effects. In practice, this same principle is used a lot. Especially when all we need from other object is what it can do, regardless of what type it really is.&lt;/p&gt;

&lt;h1&gt;
  
  
  Struct
&lt;/h1&gt;

&lt;p&gt;A &lt;code&gt;struct&lt;/code&gt; is very similar to a &lt;code&gt;class&lt;/code&gt;. Let's see some differences:&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;class&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Has attributes and methods.&lt;/li&gt;
&lt;li&gt;The attributes and methods it has may be private.&lt;/li&gt;
&lt;li&gt;It can implement protocols.&lt;/li&gt;
&lt;li&gt;It must implement a &lt;code&gt;init&lt;/code&gt; method to initialize its attributes when it's instantiated.&lt;/li&gt;
&lt;li&gt;It can inherit from other classes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A &lt;code&gt;struct&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Has attributes and methods.&lt;/li&gt;
&lt;li&gt;The attributes and methods it has may be private.&lt;/li&gt;
&lt;li&gt;It can implement protocols.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It is not necessary&lt;/strong&gt; that it implements an &lt;code&gt;init&lt;/code&gt; method to initialize its attributes in general. An &lt;code&gt;init&lt;/code&gt; method is automatically generated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It can't inherit&lt;/strong&gt; from another &lt;code&gt;class&lt;/code&gt; or &lt;code&gt;struct&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A &lt;code&gt;class&lt;/code&gt; also can't inherit from a &lt;code&gt;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;protocol&lt;/span&gt; &lt;span class="kt"&gt;HasDescription&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;getDescription&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&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;Address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;HasDescription&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;street&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;number&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;apartment&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;floor&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;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;City&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;getDescription&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;street&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; - Apt. &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;floor&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;apartment&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; - &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDescription&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&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;City&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;HasDescription&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;name&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;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;State&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;getDescription&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDescription&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;}&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;State&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;HasDescription&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;name&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;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;getDescription&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&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;let&lt;/span&gt; &lt;span class="nv"&gt;someAddress&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Address&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;street&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Some St."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"18"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;apartment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;City&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Palermo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nv"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;State&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Buenos Aires"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nv"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Argentina"&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;someAddress&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDescription&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// Some St. 18 - Apt. 2-A - Palermo, Buenos Aires, Argentina&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In practice, the &lt;code&gt;struct&lt;/code&gt; objects are used to model small chunks of information. They can serve us to describe an address, credit card data, etc.&lt;/p&gt;

&lt;h1&gt;
  
  
  Enum
&lt;/h1&gt;

&lt;p&gt;A &lt;code&gt;struct&lt;/code&gt; or &lt;code&gt;class&lt;/code&gt; let us design based on "and". For example, &lt;code&gt;struct Person&lt;/code&gt; has &lt;code&gt;firstName&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt;, and &lt;code&gt;email&lt;/code&gt; as its members.&lt;br&gt;
An &lt;code&gt;enum&lt;/code&gt;, on the other hand, allow us design based on "or". For instance, &lt;code&gt;enum State&lt;/code&gt; has &lt;code&gt;florida&lt;/code&gt;, &lt;code&gt;california&lt;/code&gt; or &lt;code&gt;texas&lt;/code&gt; as its members.&lt;/p&gt;

&lt;p&gt;Unlike &lt;code&gt;enum&lt;/code&gt; in languages as C, &lt;code&gt;enum&lt;/code&gt; in Swift can get to be really complex. Anyway, we'll use basic functionality on these examples.&lt;/p&gt;

&lt;p&gt;Let's see a basic 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="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;User&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;name&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;var&lt;/span&gt; &lt;span class="nv"&gt;credentialType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CredentialType&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nv"&gt;name&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="nv"&gt;credentialType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CredentialType&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;credentialType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;credentialType&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// enum CredentialType defines how a user created their account.&lt;/span&gt;
&lt;span class="kd"&gt;enum&lt;/span&gt; &lt;span class="kt"&gt;CredentialType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;facebook&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;apple&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;google&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the &lt;code&gt;CredentialType&lt;/code&gt; enum, we can say that for instance, a user registered using &lt;code&gt;email&lt;/code&gt;, or &lt;code&gt;facebook&lt;/code&gt;, or &lt;code&gt;apple&lt;/code&gt;, or &lt;code&gt;google&lt;/code&gt;. It becomes impossible that a user has registered using more than one of these values, and this code makes it impossible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Switch using enum
&lt;/h2&gt;

&lt;p&gt;Ok, let's try to use &lt;code&gt;CredentialType&lt;/code&gt; for our case. The most typical way of using an &lt;code&gt;enum&lt;/code&gt; in our logic, is by using the keyword &lt;code&gt;switch&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;func&lt;/span&gt; &lt;span class="nf"&gt;isSocialNetworkUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&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="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;credentialType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;facebook&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apple&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;google&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&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;Also note that we could have defined that function inside &lt;code&gt;User&lt;/code&gt;. And that we're combining several options inside the same &lt;code&gt;case&lt;/code&gt;, but they could have also been written with a &lt;code&gt;case&lt;/code&gt; for each option (facebook, apple, google).&lt;/p&gt;

&lt;h2&gt;
  
  
  Methods and attributes in &lt;code&gt;enum&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;An &lt;code&gt;enum&lt;/code&gt; may have methods and attributes, like a class. The most common pattern for doing that is including a &lt;code&gt;switch self&lt;/code&gt; clause inside the method or attribute. Let's see 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="kd"&gt;enum&lt;/span&gt; &lt;span class="kt"&gt;Country&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;argentina&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;germany&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;england&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;usa&lt;/span&gt;

    &lt;span class="c1"&gt;// We'll create a computed property. A `getter`.&lt;/span&gt;
    &lt;span class="c1"&gt;// Note that this is the same as write&lt;/span&gt;
    &lt;span class="c1"&gt;//&lt;/span&gt;
    &lt;span class="c1"&gt;// func isEuropean() -&amp;gt; Bool { ... }&lt;/span&gt;
    &lt;span class="c1"&gt;//&lt;/span&gt;
    &lt;span class="c1"&gt;// But when we call it, we won't use parenthesis, exactly as if we would be working with a property.&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;isEuropean&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// switch self is VERY common in these cases&lt;/span&gt;
        &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// If we're calling this property for cases `germany` or `england`.&lt;/span&gt;
        &lt;span class="c1"&gt;// then we'll return `true`&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;germany&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;england&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="c1"&gt;// However, if we're doing this from any other case of this enum, then&lt;/span&gt;
        &lt;span class="c1"&gt;// we'll return false&lt;/span&gt;
        &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Similar to the previous case but with a String&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;nombre&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="k"&gt;switch&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;argentina&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Argentina"&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;germany&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Germany"&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;england&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"England"&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;usa&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"United States of America"&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="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Country&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isEuropean&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;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; is European"&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;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; is NOT European"&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="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Country&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;germany&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Germany is European&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another curious thing about &lt;code&gt;enum&lt;/code&gt; is that when we need to use one of them, like in the case of &lt;code&gt;describe&lt;/code&gt;, there is not necessary to specify the type. For instance, &lt;code&gt;Country.germany&lt;/code&gt; could have been just &lt;code&gt;.germany&lt;/code&gt;. (note: This is not specific of enums, but it's notorious and easy to recognize for these)&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="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argentina&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// It's the same to say describe(country: Country.argentina) and it feels more natural.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  rawValue
&lt;/h2&gt;

&lt;p&gt;There is an alternative way to define the country name, as we've seen in the previous example, and it's by using a &lt;code&gt;rawValue&lt;/code&gt;. Each &lt;code&gt;enum&lt;/code&gt; can have a single &lt;code&gt;rawValue&lt;/code&gt; for each of its cases, and all of them must be of the same type.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rawValue&lt;/code&gt; let us not only get a value associated to each &lt;code&gt;case&lt;/code&gt;, but also create a case for the enum based on that associated value.&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;enum&lt;/span&gt; &lt;span class="kt"&gt;State&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="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;florida&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Florida"&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;newYork&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"New York"&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;california&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"California"&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;state1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;State&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;florida&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;"State 1 is &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;state1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rawValue&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// We use the rawValue to get the value for that state&lt;/span&gt;
&lt;span class="c1"&gt;// this will print "State 1 is Florida"&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;state2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;State&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;rawValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"California"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Here, we're doing the inverse process&lt;/span&gt;
&lt;span class="c1"&gt;// instead of getting the rawValue of a state,&lt;/span&gt;
&lt;span class="c1"&gt;// we create the state based on its rawValue&lt;/span&gt;
&lt;span class="c1"&gt;//&lt;/span&gt;
&lt;span class="c1"&gt;// We can make mistakes, so there is no guarantee that it exists&lt;/span&gt;
&lt;span class="c1"&gt;// a state with that name, the type of state2&lt;/span&gt;
&lt;span class="c1"&gt;// is `State?`, an optional State.&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;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state2&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;"We could get state2 and its &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rawValue&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&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;"We couldn't get state2, and it's nil"&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;h2&gt;
  
  
  Associated value
&lt;/h2&gt;

&lt;p&gt;This is the last case we'll see here about &lt;code&gt;enum&lt;/code&gt;. I'd like to explain them here because they are something very used in Swift, although it's not inside the scope of what's needed to continue with the course. I mean, it's not necessary to grasp the following lessons. However, knowing this can be very useful in your careers.&lt;/p&gt;

&lt;p&gt;Each enum case can have associated values. For example, let's imagine a &lt;code&gt;Cookie&lt;/code&gt; enum. Each cookie might be represented using a &lt;code&gt;enum&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;enum&lt;/span&gt; &lt;span class="kt"&gt;Cookie&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Each case may have any number of associated values. Each one may optionally have&lt;/span&gt;
    &lt;span class="c1"&gt;// an associated name and may be of the type we need.&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nf"&gt;chocChip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;dough&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;DoughType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nf"&gt;stuffed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;stuff&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;StuffType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;dough&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;DoughType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;fortune&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;cookieDescription&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="k"&gt;switch&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// We can get associated values using the keyword `let`.&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chocChip&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;dough&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Chocolate chips cookie with dough of type &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;dough&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;doughDescription&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stuffed&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;stuff&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;dough&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Stuffed cookie of &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;stuff&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stuffDescription&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;, flavor &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;dough&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;doughDescription&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;fortune&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Fortune Cookie"&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="kd"&gt;enum&lt;/span&gt; &lt;span class="kt"&gt;StuffType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;vanilla&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;strawberry&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;stuffDescription&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="k"&gt;switch&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;vanilla&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"vanilla"&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;strawberry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"strawberry"&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="kd"&gt;enum&lt;/span&gt; &lt;span class="kt"&gt;DoughType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;vanilla&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;chocolate&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;doughDescription&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="k"&gt;switch&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;vanilla&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"vanilla"&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;chocolate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"chocolate"&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="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;CookieSet&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;cookies&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Cookie&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;describe&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="s"&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="s"&gt;"Cookies:"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;cookie&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;cookies&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;cookie&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cookieDescription&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;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;cookies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;CookieSet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;cookies&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;fortune&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stuffed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;relleno&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;frambuesa&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;saborDeMasa&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chocolate&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stuffed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;relleno&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;frambuesa&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;saborDeMasa&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chocolate&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stuffed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;relleno&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;frambuesa&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;saborDeMasa&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chocolate&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chocChip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;saborDeMasa&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chocolate&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chocChip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;saborDeMasa&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;vanilla&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chocChip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;saborDeMasa&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;vanilla&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fortune&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fortune&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chocChip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;saborDeMasa&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chocolate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="n"&gt;cookies&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;//    -&lt;/span&gt;
&lt;span class="c1"&gt;//    Cookies:&lt;/span&gt;
&lt;span class="c1"&gt;//    Fortune&lt;/span&gt;
&lt;span class="c1"&gt;//    Stuffed cookie of strawberry, flavor chocolate&lt;/span&gt;
&lt;span class="c1"&gt;//    Stuffed cookie of strawberry, flavor chocolate&lt;/span&gt;
&lt;span class="c1"&gt;//    Stuffed cookie of strawberry, flavor chocolate&lt;/span&gt;
&lt;span class="c1"&gt;//    Chocolate chips cookie with dough of type chocolate&lt;/span&gt;
&lt;span class="c1"&gt;//    Chocolate chips cookie with dough of type vainilla&lt;/span&gt;
&lt;span class="c1"&gt;//    Chocolate chips cookie with dough of type vainilla&lt;/span&gt;
&lt;span class="c1"&gt;//    Fortune&lt;/span&gt;
&lt;span class="c1"&gt;//    Fortune&lt;/span&gt;
&lt;span class="c1"&gt;//    Chocolate chips cookie with dough of type chocolate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Closures
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;closure&lt;/code&gt; are also called anonymous functions. We'll see first the concept of function as a data type.&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions as a data type
&lt;/h2&gt;

&lt;p&gt;In Swift, Functions are a type of data, such as &lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;Double&lt;/code&gt;, &lt;code&gt;Bool&lt;/code&gt;, a &lt;code&gt;class&lt;/code&gt;, &lt;code&gt;struct&lt;/code&gt;, or &lt;code&gt;enum. This implies that we could take a function a send it as an argument for another function, or make a function return another function as a result. Or have a&lt;/code&gt;struct` where one of its attributes is a function. This is actually a bit weird when we first see it, but it's actually pretty common in modern languages.&lt;/p&gt;

&lt;p&gt;To convert a function into its related data type, we need to pay attention to the types of its input and output. So, the &lt;code&gt;sum&lt;/code&gt; function:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
func sum(x: Int, y: Int) -&amp;gt; Int { ... }&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Is of type &lt;code&gt;(Int, Int) -&amp;gt; Int&lt;/code&gt; because it receives to &lt;code&gt;Int&lt;/code&gt; and returns an &lt;code&gt;Int&lt;/code&gt;. Let's see other examples:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
func sum(x: Int, y: Int) -&amp;gt; Int { ... } // (Int, Int) -&amp;gt; Int&lt;br&gt;
func describe(_ person: Persona) { ... } // (Persona) -&amp;gt; Void&lt;br&gt;
func printCurrentTime() { ... } // () -&amp;gt; Void&lt;br&gt;
func getCurrentDate() -&amp;gt; String { ... } // () -&amp;gt; String&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And, as we said, a Function data type can be used as an argument for other functions:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`swift&lt;br&gt;
struct Person {&lt;br&gt;
    let id: Int&lt;br&gt;
    let name: String&lt;br&gt;
    let role: PersonRole?&lt;br&gt;
    let age: Int&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;enum PersonRole {&lt;br&gt;
    case developer, projectManager, teacher, doctor&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;func printAdults(_ people: [Person]) {&lt;br&gt;
    for person in people {&lt;br&gt;
        if person.age &amp;gt;= 18 {&lt;br&gt;
            print("(person.id) - (person.name)")&lt;br&gt;
        }&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;let people = [&lt;br&gt;
    Person(id: 1, name: "Franco", role: .teacher, age: 34),&lt;br&gt;
    Person(id: 2, name: "Gimena", role: .projectManager, age: 24),&lt;br&gt;
    Person(id: 3, name: "Gonzalo", role: .teacher, age: 26),&lt;br&gt;
    Person(id: 4, name: "Noelia", role: .developer, age: 29),&lt;br&gt;
    Person(id: 5, name: "Pablo", role: nil, age: 15),&lt;br&gt;
    Person(id: 6, name: "Lourdes", role: .doctor, age: 29),&lt;br&gt;
]&lt;/p&gt;

&lt;p&gt;printAdults(people)&lt;br&gt;
// This will work correctly&lt;br&gt;
//&lt;br&gt;
// However, we don't have a way to provide 'flexibility' to the algorithm. I mean,&lt;br&gt;
// inside the function printAdults we filter by age, and then we print the result.&lt;br&gt;
// If we would like to change the filter criteria, we'd need to write a completely new function.&lt;br&gt;
// Let's convert this function in something more flexible:&lt;/p&gt;

&lt;p&gt;// We are "injecting" a function into another function as an argument&lt;br&gt;
func print(_ people: [Person], who matchesCriteria: (Person) -&amp;gt; Bool) {&lt;br&gt;
    for person in people {&lt;br&gt;
        if matchesCriteria(person) {&lt;br&gt;
            print("(person.id) - (person.name)")&lt;br&gt;
        }&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;func isAdult(_ person: Person) -&amp;gt; Bool {&lt;br&gt;
    return person.age &amp;gt;= 18&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;print(people, who: isAdult) // Exactly the same result. We're sending the function as an argument in this case.&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Anonymous functions
&lt;/h2&gt;

&lt;p&gt;Now it's time, with this introduction we can start talking about anonymous functions. An anonymous function, or &lt;strong&gt;closure&lt;/strong&gt; is a function that lacks a name. It's as simple as it sounds. And the best context for using them is to send them to other functions. For example, in this case, I could have decided that it didn't make sense to define a new function just to determine is a person is an adult.&lt;/p&gt;

&lt;p&gt;Let's define it as an anonymous function:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
print("Printing adult people using a closure (1):")&lt;br&gt;
imprimir(&lt;br&gt;
    people,&lt;br&gt;
    who: { (person: Person) -&amp;gt; Bool in&lt;br&gt;
        return person.age &amp;gt;= 18&lt;br&gt;
    }&lt;br&gt;
)&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is a closure, and there are a couple of different ways to define one, here are more examples: &lt;a href="https://fuckingclosuresyntax.com/"&gt;https://fuckingclosuresyntax.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For now, let's see the transformation step by step&lt;/p&gt;

&lt;p&gt;We have this function:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
 func isAdult(_ person: Person) -&amp;gt; Bool {&lt;br&gt;
     return person.age &amp;gt;= 18&lt;br&gt;
 }&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Step 1: We remove the &lt;code&gt;func&lt;/code&gt; keyword and its name:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
 (_ person: Person) -&amp;gt; Bool {&lt;br&gt;
     return person.age &amp;gt;= 18&lt;br&gt;
 }&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Step 2: In case its parameters have a different internal and external names, we will only use its internal ones:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
 (person: Person) -&amp;gt; Bool {&lt;br&gt;
     return person.age &amp;gt;= 18&lt;br&gt;
 }&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Step 3: We move the curly bracket to the beginning of the definition, and in its place, we will put &lt;code&gt;in&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
 { (person: Person) -&amp;gt; Bool in&lt;br&gt;
     return person.age &amp;gt;= 18&lt;br&gt;
 }&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Perfect! This is enough to correctly define a closure. We can use this closure as it is right now, but I'll show you some extra steps we can take to shorten this definition even more. Again, this is completely optional:&lt;/p&gt;

&lt;p&gt;Extra step 1: We remove the data type, as the compile can infer it based on the context for most situations:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
 { (person) in&lt;br&gt;
     return person.age &amp;gt;= 18&lt;br&gt;
 }&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Extra step 2: We remove the parenthesis around the arguments:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
 { person in&lt;br&gt;
     return person.age &amp;gt;= 18&lt;br&gt;
 }&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Extra step 3: If the closure has a single sentence, we can remove the &lt;code&gt;return&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
 { person in person.age &amp;gt;= 18 }&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Extra step 4: Instead of using the arguments names (in this case &lt;code&gt;person&lt;/code&gt;), we can refer to the arguments by its order. For instance, instead of &lt;code&gt;person&lt;/code&gt;, we can use &lt;code&gt;$0&lt;/code&gt;. If we had two arguments, the first of them would be &lt;code&gt;$0&lt;/code&gt; and the second one &lt;code&gt;$1&lt;/code&gt;. If we had four, they would be &lt;code&gt;$0&lt;/code&gt;, &lt;code&gt;$1&lt;/code&gt;, &lt;code&gt;$2&lt;/code&gt;, &lt;code&gt;$3&lt;/code&gt;, and so forth:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
 { $0.age &amp;gt;= 18 }&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And that's the minimum expression for this closure&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
print("Adult people who are teachers")&lt;br&gt;
print(people, who: { $0.age &amp;gt;= 18 &amp;amp;&amp;amp; $0.role == .teacher })&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If we had a closure as the last argument for a function, we can move it outside the function call. Let's make it clearer with an example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
print("Adult people who are teachers (2)")&lt;br&gt;
print(people) { $0.age &amp;gt;= 18 &amp;amp;&amp;amp; $0.role == .teacher } // Exactly the same as the previous example&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Map, filter, sorted and forEach
&lt;/h2&gt;

&lt;p&gt;There are some functions inside the Swift standard library that take other functions as their arguments, especially when working with &lt;code&gt;Array&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;map&lt;/code&gt; is a function that let us transform each element of an array into another element by passing it a function that actually performs the transformation.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;filter&lt;/code&gt; is a function that let us filter an array by passing a function that returns &lt;code&gt;true&lt;/code&gt; in case the element should be included into the result array, or &lt;code&gt;false&lt;/code&gt; otherwise.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sorted&lt;/code&gt; is a function that let us sort an array. It works similarly to the &lt;code&gt;filter&lt;/code&gt; function, we will get two elements and we'll return &lt;code&gt;true&lt;/code&gt; in case the first element should be first in the result array and which second.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;forEach&lt;/code&gt;, let us iterate over an array, performing anything based on the origin array. This function won't return a new array, unlike &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt; and &lt;code&gt;sorted&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's important to note that all these functions (except &lt;code&gt;forEach&lt;/code&gt;) return a new array. They don't modify the origin array.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`swift&lt;br&gt;
let adultPeople = people.filter { $0.age &amp;gt;= 18 }&lt;br&gt;
let names = people.map { $0.name }&lt;br&gt;
let peopleSortedByAge = people.sorted { $0.age &amp;lt; $1.age }&lt;/p&gt;

&lt;p&gt;// We can also "chain" these functions, because each of them will return a new &lt;code&gt;Array&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;print("Chained functions:")&lt;/p&gt;

&lt;p&gt;people&lt;br&gt;
    .filter { $0.age &amp;gt;= 18 } // We'll only take into account adult people&lt;br&gt;
    .sorted { $0.age &amp;lt; $1.age } // We'll then sort them by age&lt;br&gt;
    .map { $0.name } // And we'll extract their names&lt;br&gt;
    .forEach { print($0) } // Finally, we'll print their names&lt;br&gt;
// Gimena&lt;br&gt;
// Gonzalo&lt;br&gt;
// Noelia&lt;br&gt;
// Lourdes&lt;br&gt;
// Franco&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Extensions
&lt;/h1&gt;

&lt;p&gt;Extensions let us &lt;em&gt;extend&lt;/em&gt; an existent type to add new functionality to it. This functionality may be computed properties or methods.&lt;/p&gt;

&lt;p&gt;Let's consider a simple example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`swift&lt;br&gt;
struct Address {&lt;br&gt;
    let street: String&lt;br&gt;
    let number: String&lt;br&gt;
    let city: String&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;extension Address {&lt;br&gt;
    var addressDescription: String {&lt;br&gt;
        return "(street) (number) - (city)"&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;let address = Address(street: "Rivadavia", number: "185", city: "Palermo")&lt;br&gt;
print(address.addressDescription) // Rivadavia 185 - Palermo&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And that's exactly the same as this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`swift&lt;br&gt;
 struct Address {&lt;br&gt;
     let street: String&lt;br&gt;
     let number: String&lt;br&gt;
     let city: String&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; var addressDescription: String {
     return "\(street) \(number) - \(city)"
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

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

&lt;p&gt;An interesting use case for &lt;code&gt;extension&lt;/code&gt; is to extend native types like &lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;Double&lt;/code&gt; or &lt;code&gt;String&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`swift&lt;br&gt;
extension Int {&lt;br&gt;
    func isBigger(than anotherNumber: Int) -&amp;gt; Bool {&lt;br&gt;
        return self &amp;gt; anotherNumber&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;if 10.isBigger(than: 5) {&lt;br&gt;
    print("10 is bigger than 5")&lt;br&gt;
}&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Typealias
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;typealias&lt;/code&gt; are basically that, alias for types. This means that we can refer to an existing type with a new name. For example, let's suppose we're coding an app that handles users, where each user has an identifier. This user ID is an &lt;code&gt;Int&lt;/code&gt;. However, a &lt;code&gt;typealias&lt;/code&gt; can be even better, so we are sure we're talking about the identifier of a user.&lt;/p&gt;

&lt;p&gt;Remember that a good code is easy to extend and easy to understand.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`swift&lt;br&gt;
typealias UserID = Int&lt;/p&gt;

&lt;p&gt;struct User {&lt;br&gt;
    let id: UserID&lt;br&gt;
    let name: String&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;typealias BuildingID = Int&lt;br&gt;
typealias Address = (street: String, number: String, city: String, state: String, country: String)&lt;/p&gt;

&lt;p&gt;struct Building {&lt;br&gt;
    let id: BuildingID&lt;br&gt;
    let ownerId: UserID&lt;br&gt;
    let address: Address&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;let office = Building(&lt;br&gt;
    id: 1,&lt;br&gt;
    ownerId: 10,&lt;br&gt;
    address: (&lt;br&gt;
        street: "Rivadavia",&lt;br&gt;
        number: "18451 PB Torre 2",&lt;br&gt;
        city: "Moron",&lt;br&gt;
        state: "Buenos Aires",&lt;br&gt;
        country: "Argentina"&lt;br&gt;
    )&lt;br&gt;
)&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Note that we could have done exactly the same without &lt;code&gt;typealias&lt;/code&gt;. In general (except for advanced use cases we won't cover during this course), &lt;code&gt;typealias&lt;/code&gt; bring clarity to the code, making our intention even more evident and clear to the other developers.&lt;/p&gt;

&lt;h1&gt;
  
  
  Exercises
&lt;/h1&gt;

&lt;p&gt;We want to develop an application for organizing trips. The idea for the app is that we will have a list of possible destinations. The user can select their favorite destinations and save them, so they could then see those saved destinations in another list.&lt;/p&gt;

&lt;p&gt;This exercise consists in developing the data structures needed to support the application use cases. It's required that, least:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use classes, structs or enums to the entities &lt;code&gt;User&lt;/code&gt;, &lt;code&gt;Address&lt;/code&gt;, &lt;code&gt;Place&lt;/code&gt;, &lt;code&gt;Landmark&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Allow getting the favorites &lt;code&gt;Place&lt;/code&gt; and &lt;code&gt;Landmark&lt;/code&gt; objects for a certain user.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use your creativity and try to get the exercise as complete as possible.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Introduction to Swift - Part 1</title>
      <dc:creator>Fernando Martín Ortiz</dc:creator>
      <pubDate>Sun, 02 May 2021 21:51:55 +0000</pubDate>
      <link>https://dev.to/fmo91/introduction-to-swift-part-1-10pc</link>
      <guid>https://dev.to/fmo91/introduction-to-swift-part-1-10pc</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This article is part of the course&lt;/em&gt; &lt;strong&gt;Introduction to iOS using UIKit&lt;/strong&gt; &lt;em&gt;I've given many times in the past. The course was originally in Spanish, and I decided to release it in English so more people can read it and hopefully it will help them.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Swift overview
&lt;/h1&gt;

&lt;p&gt;Swift is a programming language developed by Apple to be used in application development for Apple platforms, as a replacement for (or in combination to) Objective-C, its previous recommended language. This way, and using Xcode, we can develop apps for iOS, iPad OS, MacOS, WatchOS and TVOS. Since 2015, Swift is also Open Source, being maintained not just by Apple, but by a open development community.&lt;/p&gt;

&lt;p&gt;Swift is multi-paradigm, which means we can use it to develop object oriented code (OOP), functional programming, scripting, etc. Swift adapts to our preferred programming style, although the community has agreed on some best practices and a way to develop code following mainly an object oriented approach.&lt;/p&gt;

&lt;p&gt;Swift is modern, which means it includes features from other modern languages, as we'll see during this course. What's more, it continues being added more features during the years.&lt;/p&gt;

&lt;p&gt;Swift is simple to learn, but it can be as complex and go as low level as we need it to go.&lt;/p&gt;

&lt;p&gt;During this course we'll learn the Swift syntax and usage examples. But not in depth. We'll learn the necessary syntax in order to start developing iOS application. However, it's important to note that the language can be much more complex than what we'll learn here. This course isn't intended to be comprehensive, but to bring the needed tools to start.&lt;/p&gt;

&lt;h1&gt;
  
  
  Variables
&lt;/h1&gt;

&lt;p&gt;Variables are present in most (if not all) programming languages, and are names (also called identifiers), which contain a value.&lt;br&gt;
In Swift, variables are declared using the keyword &lt;code&gt;var&lt;/code&gt;, followed by a &lt;code&gt;=&lt;/code&gt; and the value it contains. For instance:&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;"Fernando"&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;street&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Rivadavia Av."&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;catLegsCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Something that could be curious in these declarations is that we aren't specifying their type. That's because Swift includes a feature called &lt;strong&gt;type inference&lt;/strong&gt;. Type inference let us declare variables without having to be explicit about their types, because Swift is "intelligent" enough to figure out that a &lt;code&gt;4&lt;/code&gt; is an &lt;code&gt;Int&lt;/code&gt;, and that &lt;code&gt;"Fernando"&lt;/code&gt; is a &lt;code&gt;String&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  We can be explicit
&lt;/h2&gt;

&lt;p&gt;Of course, there are moments where it could be useful to be explicit about the data type of a variable. For instance, if we're talking about a price.&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;price1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could want to be explicit about the type because the price is a decimal value, and it's not an &lt;code&gt;Int&lt;/code&gt;. In Swift, we can specify the data type using &lt;code&gt;: &amp;lt;DataType&amp;gt;&lt;/code&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;price2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;2.50&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Constants
&lt;/h2&gt;

&lt;p&gt;Let's suppose we have a variable with a value that we &lt;strong&gt;know&lt;/strong&gt; it won't change. For example, a cat has four legs. If we want to define the number of legs a cat has, we can use a constant. In order to so, we'll replace &lt;code&gt;var&lt;/code&gt; by &lt;code&gt;let&lt;/code&gt;, transforming that variable into a constant. A constant is a variable to which we &lt;strong&gt;can only associate a value once&lt;/strong&gt;. If we want to update its value afterwards, we'll get an error.&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;leavesInATree&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;leavesInATree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;
&lt;span class="n"&gt;leavesInATree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt; &lt;span class="c1"&gt;// We won't get any errors if we modify this value.&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;catLegsCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;

&lt;span class="c1"&gt;// The next line won't compile.&lt;/span&gt;
&lt;span class="c1"&gt;// catLegsCount = 5 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While naming a variable, it's important to be very explicit on its meaning. This means: &lt;strong&gt;do not abbreviate&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;lbl&lt;/code&gt; isn't a good variable name. &lt;code&gt;label&lt;/code&gt; is.&lt;br&gt;
&lt;code&gt;chCnt&lt;/code&gt; isn't a good variable name. &lt;code&gt;charactersCount&lt;/code&gt; is.&lt;br&gt;
&lt;code&gt;i&lt;/code&gt; isn't a good variable name. &lt;code&gt;index&lt;/code&gt; is.&lt;/p&gt;

&lt;p&gt;Every programming language has it's own "correct" way to write code with. We know it as &lt;strong&gt;best practices&lt;/strong&gt;. In Swift, the best practice is to declare variables as constants, using &lt;code&gt;let&lt;/code&gt;, and use &lt;code&gt;var&lt;/code&gt; only when it's strictly needed. This will  save us from potential headaches, because we know that the variable we're using that was declared using &lt;code&gt;let&lt;/code&gt;, hasn't been modified. For complex functions, this can be critical.&lt;/p&gt;
&lt;h1&gt;
  
  
  Data Types
&lt;/h1&gt;

&lt;p&gt;As we've seen in the last section, although it isn't necessary to be explicit about a variable's data type (because the compiler &lt;strong&gt;infers&lt;/strong&gt; the data type), we can choose to be explicit and specify it anyway. In this section, we'll do it that way.&lt;br&gt;
Swift comes with a set of predefined data types, and let us define our custom types, as we'll see in the following sections.&lt;/p&gt;
&lt;h2&gt;
  
  
  Numeric Data Types
&lt;/h2&gt;

&lt;p&gt;Swift defines the following main numeric data types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Int&lt;/code&gt;: Integer number.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Double&lt;/code&gt;: A decimal number.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Float&lt;/code&gt;: A decimal number, but with less capacity than a &lt;code&gt;Double&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;legs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;30.50&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;temperature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Float&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;20.2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Bool
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Bool&lt;/code&gt; is a boolean value. It can either be &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. Those are only two possibilities.&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;let&lt;/span&gt; &lt;span class="nv"&gt;enabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;isReady&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  String
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;String&lt;/code&gt; is a list of characters. It's used to define texts and words. For instance, a person name or a street name could be defined as &lt;code&gt;String&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="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Fernando"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A curious fact about &lt;code&gt;String&lt;/code&gt; values in Swift is that emojis are valid &lt;code&gt;String&lt;/code&gt; values!&lt;br&gt;
Why? Why not? 🤷&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;let&lt;/span&gt; &lt;span class="nv"&gt;fancyHelloWorld&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"👋 🌎!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Print
&lt;/h1&gt;

&lt;p&gt;Sometimes we need to print a value in the console. In Swift, we can do that using the &lt;code&gt;print()&lt;/code&gt; function. For example, a "Hello, World!" program can be written like this:&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you execute this program using the button in the left bar, a message will appear in the console that is at the bottom of the screen.&lt;/p&gt;

&lt;h1&gt;
  
  
  Comments
&lt;/h1&gt;

&lt;p&gt;Comments can be single-line or multi-line.&lt;/p&gt;

&lt;h2&gt;
  
  
  Single-liner comments
&lt;/h2&gt;

&lt;p&gt;They begin with a &lt;code&gt;//&lt;/code&gt; and only occupy one line.&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;let&lt;/span&gt; &lt;span class="nv"&gt;mensaje&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello, world!"&lt;/span&gt; &lt;span class="c1"&gt;// This is a message&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Multi-line comments
&lt;/h2&gt;

&lt;p&gt;They begin with a &lt;code&gt;/*&lt;/code&gt; and end with &lt;code&gt;*/&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="cm"&gt;/* This line
 prints the
 Hello, World! message */&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;mensaje&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Interpolation
&lt;/h1&gt;

&lt;p&gt;How do we create a &lt;code&gt;String&lt;/code&gt; from other variables? An easy way to do it is by concatenating the parts.&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;let&lt;/span&gt; &lt;span class="nv"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Fernando"&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Ortiz"&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;fullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="c1"&gt;// Fernando Ortiz&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The alternative, and the most common way to do it in Swift, is by using &lt;strong&gt;interpolation&lt;/strong&gt;. A &lt;code&gt;String&lt;/code&gt; interpolation consists in "embedding" variables inside a &lt;code&gt;String&lt;/code&gt;. This is done by entering &lt;code&gt;\(&amp;lt;variable&amp;gt;)&lt;/code&gt; inside the &lt;code&gt;String&lt;/code&gt;. For instance:&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;let&lt;/span&gt; &lt;span class="nv"&gt;middleName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Martín"&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;experience&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;description&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"My name is &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;middleName&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; and I have &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;experience&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; years of experience as an iOS developer."&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;description&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// "My name is Fernando Martín Ortiz and I have 7 years of experience as an iOS developer."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we're also introducing an &lt;code&gt;Int&lt;/code&gt; in the middle of the &lt;code&gt;String&lt;/code&gt; and we aren't getting problems. That's another advantage of &lt;code&gt;String&lt;/code&gt; interpolation.&lt;/p&gt;

&lt;h1&gt;
  
  
  Collections
&lt;/h1&gt;

&lt;p&gt;Let's collections in Swift to the data types that contain other data types on them and that we can iterate over them on some way.&lt;/p&gt;

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

&lt;p&gt;Also known as lists or vectors, they are data types that store values in a sequential way. What's interesting about Arrays in Swift is that you can't mix values of different types in an &lt;code&gt;Array&lt;/code&gt;. The simplest way of defining an array is this:&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;let&lt;/span&gt; &lt;span class="nv"&gt;students&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Federico"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Micaela"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Aldana"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Oscar"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we are just using &lt;code&gt;String&lt;/code&gt; values. An &lt;code&gt;Array&lt;/code&gt; with the values &lt;code&gt;["Fernando", 28, true]&lt;/code&gt; won't be valid, unless the &lt;code&gt;Array&lt;/code&gt; is of type &lt;code&gt;[Any]&lt;/code&gt;. The &lt;code&gt;Any&lt;/code&gt; type is a type that can be of "any" type of value, but there are certain disadvantages on using it. My advice here is that, for now at least, &lt;strong&gt;AVOID USING IT&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If we want to be explicit about the type, we can also do 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;let&lt;/span&gt; &lt;span class="nv"&gt;numbers1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;129&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;numbers2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&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;122&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are many ways of declaring an empty &lt;code&gt;Array&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="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;numbersEmpty1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&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;// Preferred&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;numbersEmpty2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&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;numbersEmpty3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&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;numbersEmpty4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Useful methods in &lt;code&gt;Array&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;If we want to know the number of elements in an &lt;code&gt;Array&lt;/code&gt;, we use the property &lt;code&gt;count&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="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;numbers3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;231&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&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;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;numbers3&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; has &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;number3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; elements"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Curious fact, &lt;code&gt;String&lt;/code&gt; in Swift are actually sequences of characters, so they also have the &lt;code&gt;count&lt;/code&gt; property defined on them. Actually, most of the things we explain in this section apply also for &lt;code&gt;String&lt;/code&gt; values.&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;let&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;"Fernando"&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;nameLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&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;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; has &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;nameLength&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; characters."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want to get the first element in an &lt;code&gt;Array&lt;/code&gt;, we'll use &lt;code&gt;.first!&lt;/code&gt;, why we're using &lt;code&gt;!&lt;/code&gt; will be explained later. For now, let's say we're using &lt;code&gt;!&lt;/code&gt; to affirm that we know with &lt;strong&gt;full confidence&lt;/strong&gt; that the first element in the &lt;code&gt;Array&lt;/code&gt; will be there, because the &lt;code&gt;Array&lt;/code&gt; is not empty. Note that if we do &lt;code&gt;.first!&lt;/code&gt; and the &lt;code&gt;Array&lt;/code&gt; is in fact empty, the program will stop executing and crash.&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;let&lt;/span&gt; &lt;span class="nv"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"Tamara"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Nicolás"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Francisco"&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;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nombres&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="c1"&gt;// "Tamara"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want to know if an &lt;code&gt;Array&lt;/code&gt; is empty, we'll use the &lt;code&gt;isEmpty&lt;/code&gt; property, that's of type &lt;code&gt;Bool&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;numbersEmpty&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isEmpty&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;"It's empty!"&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;If we'd like to add an element to an &lt;code&gt;Array, we'll use the function&lt;/code&gt;append`.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
var namesToFill: [String] = []&lt;br&gt;
namesToFill.append("Fernando")&lt;br&gt;
namesToFill.append("Martín")&lt;br&gt;
print("namesToFill has \(namesToFill.count) elements") // namesToFill has 2 elements&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If we want to access an exact position in an &lt;code&gt;Array&lt;/code&gt;, we'll use what's known as a &lt;code&gt;subscript&lt;/code&gt;, and that's basically a number (the index) between square brackets. Indexes in a Swift &lt;code&gt;Array&lt;/code&gt; start from 0. Note that if we want to get an element in a position that is undefined, the program will stop executing and crash.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
let names2 = ["Aldana", "Iván", "Marcos", "Cristian"]&lt;br&gt;
let thirdName = names2[2] // "Marcos"&lt;br&gt;
// This will fail when executed.&lt;br&gt;
// let quintoNombre = nombres2[4]&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Dictionaries
&lt;/h1&gt;

&lt;p&gt;A Swift &lt;code&gt;Dictionary&lt;/code&gt; is a container that associate identifiers to values. They are &lt;strong&gt;key-value&lt;/strong&gt; structures where the key is generally a &lt;code&gt;String&lt;/code&gt; and the value can be anything. In contrast to &lt;code&gt;Array&lt;/code&gt;, where the &lt;code&gt;Any&lt;/code&gt; type was generally discouraged, creating &lt;code&gt;Dictionary&lt;/code&gt; objects where the key is &lt;code&gt;String&lt;/code&gt; and the value is &lt;code&gt;Any&lt;/code&gt; is perfectly normal and widely used.&lt;br&gt;
The &lt;code&gt;Dictionary&lt;/code&gt; type is similar to what a &lt;code&gt;Map&lt;/code&gt; is in other programming languages.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
let person: [String: Any] = [ // Note that when we define a dictionary with&lt;/code&gt;Any` as its value type, it can be anything.&lt;br&gt;
    "firstName": "Richard",&lt;br&gt;
    "lastName": "Brown",&lt;br&gt;
    "age": 27,&lt;br&gt;
    "position": "Project Manager"&lt;br&gt;
]&lt;/p&gt;

&lt;p&gt;let family: [String: String] = [ // If we define the dictionary as [String:String], values can only be String.&lt;br&gt;
    "mother": "Marge",&lt;br&gt;
    "father": "Homer",&lt;br&gt;
    "son": "Bart",&lt;br&gt;
    "daughter": "Lisa",&lt;br&gt;
    "baby": "Maggie",&lt;br&gt;
    "dog": "Santa's Little Helper"&lt;br&gt;
]&lt;/p&gt;

&lt;p&gt;let emptyDictionary: [String: Any] = [:] // The &lt;code&gt;:&lt;/code&gt; sign must be there, so the compiler won't confuse this with an empty &lt;code&gt;Array&lt;/code&gt;.&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The advantage of using key-value objects is that we can now access to each value by its key. In the Family example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
let mom = family["mother"]! // "Marge"&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;!&lt;/code&gt; sign is necessary because we need to tell the compiler that there is an actual value associated to that key.&lt;/p&gt;
&lt;h1&gt;
  
  
  For
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;for&lt;/code&gt; loop is a very well known structure in general programming languages. In Swift it's a bit different. The &lt;code&gt;for&lt;/code&gt; loop let us execute a code fragment a predefined number of times. In practice, the most common use case for it, is to iterate over a collection, such as an &lt;code&gt;Array&lt;/code&gt; or a &lt;code&gt;Dictionary&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
// We'll use these values for the examples.&lt;br&gt;
let names = ["Ayelén", "Lautaro", "Natalia", "Sergio", "Gerardo"]&lt;br&gt;
let person: [String: Any] = [&lt;br&gt;
    "firstName": "Richard",&lt;br&gt;
    "lastName": "Brown",&lt;br&gt;
    "age": 27,&lt;br&gt;
    "position": "Project Manager"&lt;br&gt;
]&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  for-in - Array
&lt;/h2&gt;

&lt;p&gt;To iterate over an &lt;code&gt;Array&lt;/code&gt;, we can use a &lt;code&gt;for-in&lt;/code&gt; loop. In this case, this will print:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
 Name: Ayelén&lt;br&gt;
 Name: Lautaro&lt;br&gt;
 Name: Natalia&lt;br&gt;
 Name: Sergio&lt;br&gt;
 Name: Gerardo&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
for name in names {&lt;br&gt;
    print("Name: \(name)")&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  for-in - Dictionary
&lt;/h2&gt;

&lt;p&gt;We can also iterate over dictionaries using the &lt;code&gt;for-in&lt;/code&gt; loop. In this case, the syntax will be a bit different, because for each element in a &lt;code&gt;Dictionary&lt;/code&gt; wi'll get a pair (&lt;code&gt;tuple&lt;/code&gt; actually, as we'll see in a later section), with the corresponding key and value. Let's see an example. this will print:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
 lastName : Gomez&lt;br&gt;
 position : Project Manager&lt;br&gt;
 firstName : Ricardo&lt;br&gt;
 age : 27&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Also note that a &lt;code&gt;Dictionary&lt;/code&gt; doesn't preserve the fields order. In this example, &lt;code&gt;lastName&lt;/code&gt; was printed third, regardless I defined it first.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
for (key, value) in person {&lt;br&gt;
    print("\(key) : \(value)")&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Ranges
&lt;/h2&gt;

&lt;p&gt;An alternative way of using &lt;code&gt;for&lt;/code&gt; loops is by define a &lt;code&gt;Range&lt;/code&gt;. A &lt;code&gt;Range&lt;/code&gt; defines a value interval.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(0 ...&amp;lt; 10)&lt;/code&gt; for instance, defines a range from 0 to 10, not including 10.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(0 ... 10)&lt;/code&gt; defines a range from 0 to 10, 10 included.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Will print:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
 Number: 0&lt;br&gt;
 Number: 1&lt;br&gt;
 Number: 2&lt;br&gt;
 Number: 3&lt;br&gt;
 Number: 4&lt;br&gt;
 Number: 5&lt;br&gt;
 Number: 6&lt;br&gt;
 Number: 7&lt;br&gt;
 Number: 8&lt;br&gt;
 Number: 9&lt;br&gt;
 Number: 10&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
for number in 0 ... 10 {&lt;br&gt;
    print("Number: \(number)")&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can use the &lt;code&gt;count&lt;/code&gt; property in &lt;code&gt;Array&lt;/code&gt;, to iterate over it in a more "traditional" way. Note that &lt;code&gt;for index in 0 ..&amp;lt; names.count&lt;/code&gt; is very similar to something like &lt;code&gt;for var index = 0 ; index &amp;lt; names.count ; index++&lt;/code&gt;, which is a syntax that isn't valid in Swift. Furthermore, &lt;code&gt;index++&lt;/code&gt; isn't valid Swift code. An alternative to that is &lt;code&gt;index += 1&lt;/code&gt;, but anyway, the most traditional way of using &lt;code&gt;for&lt;/code&gt; isn't valid in Swift.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;swift&lt;br&gt;
for index in 0 ..&amp;lt; names.count {&lt;br&gt;
    print("Name: \(names[index])")&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  While
&lt;/h1&gt;

&lt;p&gt;The &lt;code&gt;while&lt;/code&gt; loop is widely used in programming languages. However, if I have to be honest, in my almost 7 years as iOS developer, I don't remember having to ever use it. Most of the times, some sort of &lt;code&gt;for&lt;/code&gt; is enough, and it's better also, because every time we use &lt;code&gt;whie&lt;/code&gt;, we are in the risk of accidentally entering into an infinite loop.&lt;br&gt;
In this section, I'll show you how to use &lt;code&gt;while&lt;/code&gt;. But let me repeat, it's almost unnecessary to use it.&lt;br&gt;
&lt;code&gt;while&lt;/code&gt; is a keyword what let us define a condition and a block of code. The condition will be evaluated. It it's &lt;code&gt;true&lt;/code&gt;, then the block of code will be executed&lt;code&gt;. This process will be repeated indefinitely, until the condition is evaluated as &lt;/code&gt;false`.&lt;/p&gt;

&lt;p&gt;This example will print:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Name with while: Ayelén
 Name with while: Lautaro
 Name with while: Natalia
 Name with while: Sergio
 Name with while: Gerardo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&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;"Name with while: &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&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;h1&gt;
  
  
  Functions
&lt;/h1&gt;

&lt;p&gt;Functions are blocks of code that can receive a set of parameters, the &lt;strong&gt;input&lt;/strong&gt;, and can return a value as a result, named &lt;strong&gt;output&lt;/strong&gt;.&lt;br&gt;
In Swift, functions are declared with the keyword &lt;code&gt;func&lt;/code&gt;. The structure to declare a function is the following:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;func &amp;lt;functionName&amp;gt; (&amp;lt;input1&amp;gt;: &amp;lt;InputType1&amp;gt;, &amp;lt;input2&amp;gt;: &amp;lt;InputType2&amp;gt;, ..., &amp;lt;inputN&amp;gt;: &amp;lt;InputTypeN&amp;gt;) -&amp;gt; &amp;lt;OutputType&amp;gt; { &amp;lt;body&amp;gt; }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Functions may or may not have an output, as previously said. However, if we declare that the function will return a value, we need to return it using the &lt;code&gt;return&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;All of this can be a bit confusing but it's actually very intuitive. Let's see some 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="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;helloWorld&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="s"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&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;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hi, &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&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;p&gt;The &lt;code&gt;sum&lt;/code&gt; function receives two parameters, &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;, and returns the sum of both of them (it has an Output).&lt;br&gt;
The &lt;code&gt;helloWorld&lt;/code&gt; function doesn't have an input, neither an output. It only prints &lt;code&gt;"Hello, World!"&lt;/code&gt; in the console.&lt;br&gt;
The &lt;code&gt;greet&lt;/code&gt; function receives an input &lt;code&gt;name&lt;/code&gt; and doesn't return anything.&lt;/p&gt;
&lt;h2&gt;
  
  
  Calling a function
&lt;/h2&gt;

&lt;p&gt;Functions can be called (or "invoqued") by their name, and the names of their arguments explicitly. This is very important and it's different to other languages.&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;let&lt;/span&gt; &lt;span class="nv"&gt;sumResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 24&lt;/span&gt;
&lt;span class="nf"&gt;helloWorld&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Fernando"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Legibility
&lt;/h2&gt;

&lt;p&gt;Now, if you're detail-oriented people, you probably noticed that &lt;code&gt;greet(name: "Fernando")&lt;/code&gt; doesn't read so natural, to be honest. We can fix that. Don't you think it would be easier to read &lt;code&gt;greet(to: "Fernando")&lt;/code&gt;? However, that would make things worse, because inside the function we'd have a variable &lt;code&gt;to&lt;/code&gt;, with the value &lt;code&gt;"Fernando"&lt;/code&gt;.&lt;br&gt;
Actually, we aren't forced to choose between one way or the other. Swift differentiates between &lt;strong&gt;internal names&lt;/strong&gt; and &lt;strong&gt;external names&lt;/strong&gt; for function parameters. Let's do a modification here, with a new function &lt;code&gt;sayHello&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;func&lt;/span&gt; &lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="nv"&gt;name&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;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Fernando"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Much better! This new function &lt;code&gt;sayHello&lt;/code&gt; receives a single argument with an internal name &lt;code&gt;name&lt;/code&gt;, visible only inside the function body, and an external name &lt;code&gt;to&lt;/code&gt;, visible from the outside of the function (when we call it). If we don't specify different names for the internal and external ones, both will be the same, such as in the first couple of examples.&lt;/p&gt;

&lt;p&gt;Let's fix another thing. The function &lt;code&gt;sum&lt;/code&gt; looks a bit weird. Instead of &lt;code&gt;sum(x: 10, y: 14)&lt;/code&gt;, it would be more natural to say &lt;code&gt;sum(10, 14)&lt;/code&gt; or &lt;code&gt;sum(10, to: 14)&lt;/code&gt;, or something similar. Swift let us define empty external names using the keyword &lt;code&gt;_&lt;/code&gt;. The low dash let us specify that we don't want an external name for that parameter.&lt;br&gt;
Let's try to define a divide function that would look like &lt;code&gt;divide(8, by: 2)&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;func&lt;/span&gt; &lt;span class="nf"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="nv"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;by&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="c1"&gt;// 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Preconditions
&lt;/h1&gt;

&lt;p&gt;A precondition is a condition that needs to be &lt;code&gt;true&lt;/code&gt; so that a function is executed. If the precondition is &lt;code&gt;false&lt;/code&gt;, then the function will return a different value or throws an error. In Swift, we express the preconditions using the keyword &lt;code&gt;guard&lt;/code&gt;. We can read it as "Ensure this is true. Otherwise, do this instead".&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;describeDivision&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="nv"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="nv"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Dividing by zero is not possible."&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"The result of dividing &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; by &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; is &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;."&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;description1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;describeDivision&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;of&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;by&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;description1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// The result of dividing 20 by 5 is 4.&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;description2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;describeDivision&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;by&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;description2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Dividing by zero is not possible.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Best practices
&lt;/h1&gt;

&lt;p&gt;Until here, the theory of functions syntax. &lt;strong&gt;The following best practices section is entirely optional. Feel free to skip it or read it at the end of this course.&lt;/strong&gt; However, I will give you a piece of advice that I think is important.&lt;br&gt;
Why do we write functions? There are many reasons, but it's important to understand that a function is an important tool that we have to build abstractions in our code. An abstraction is in some way, to adapt a concept to natural terms, so we can reason on it in a more intuitive way. Considering we write code to define a solution to a problem in a way that not only the computer will understand (which is the simplest part), but also other people will be able to read it, understand it, and feel confident modifying it and extending it, then abstractions will allow us to improve the readability of our code.&lt;br&gt;
A good function needs to be understandable, clear, readable and modifiable. I'll list, next, a couple of best practices when writing functions.&lt;/p&gt;
&lt;h2&gt;
  
  
  Short functions
&lt;/h2&gt;

&lt;p&gt;A function shouldn't be longer than 10 or 15 lines of code, in general. If a function is longer than that, it's convenient to split it in smaller functions, and then compose those smaller functions into a bigger function. A single-line function isn't necessarily bad, if it bring clarity to the code.&lt;/p&gt;
&lt;h2&gt;
  
  
  Readable functions
&lt;/h2&gt;

&lt;p&gt;This is try not only to the function body, but also to the function invocation part. Code inside a function needs to be a well defined sequence of steps. The invocation of that function needs to be read naturally.&lt;/p&gt;
&lt;h2&gt;
  
  
  Cohesive functions
&lt;/h2&gt;

&lt;p&gt;A function must do one thing, and do it right. This is even more important than writing small functions. If a function is longer than 25 lines or code, but every line of code contribute to the same goal without adding cognitive load to the code reader, then there is no reason to modify it.&lt;/p&gt;
&lt;h1&gt;
  
  
  Tuples
&lt;/h1&gt;

&lt;p&gt;A tuple is the first custom data type we'll see during this course. Tuples are sets of data with a name and without any additional functionality (unlike classes, for instance). A tuple can be defined using the keyword &lt;code&gt;typealias&lt;/code&gt;, or when it's needed.&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;FullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;firstName&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="nv"&gt;lastName&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="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;FullName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Fernando"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Ortiz"&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;"The first name is &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; and the last name is &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// The first name is Fernando and the last name is Ortiz&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The most common use case for tuples is to let a function return more than a single value.&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;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;dividend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="nv"&gt;divisor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;quotient&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;remainder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// We assume we're not dividing by zero&lt;/span&gt;
    &lt;span class="nf"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nv"&gt;quotient&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dividend&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;divisor&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;// Integer value without the decimal values&lt;/span&gt;
        &lt;span class="nv"&gt;remainder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;dividend&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;divisor&lt;/span&gt; &lt;span class="c1"&gt;// Modulo operator returns the remainder of a division&lt;/span&gt;
    &lt;span class="p"&gt;)&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;by&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Quotient: &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;quotient&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; ;; Remainder: &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;remainder&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Quotient: 3 ;; Remainder: 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Destructuring
&lt;/h2&gt;

&lt;p&gt;Destructuring is a way of split a tuple into its components. It's done by assigning a tuple to a set of variables in parenthesis, and separated by comma:&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;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;myQuotient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;myRemainder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;by&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Quotient: &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;miQuotient&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; ;; Remainder: &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;miRemainder&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Quotient: 5 ;; Remainder: 2&lt;/span&gt;

&lt;span class="c1"&gt;// We can also ignore any of those values with a low dash.&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;quotient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&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;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;by&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="c1"&gt;// We are ignoring the remainder here&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;"The result of dividing 50 by 4 is &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;quotient&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// The result of dividing 50 by 4 is 12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Anonymous components
&lt;/h2&gt;

&lt;p&gt;A tuple can also have their components are anonymous values. Even when it's not recommendable, it's possible. In this case, we can access to its components using their index. For example: &lt;code&gt;tuple.0&lt;/code&gt; for the first element, &lt;code&gt;tuple.1&lt;/code&gt; for the second one, etc. The alternative is to use destructuring &lt;code&gt;let (first, second) = tuple&lt;/code&gt;, which is preferrable.&lt;br&gt;
Let's see a last example, generating intervals using a function that will return the lower and the upper extremes in a tuple:&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;generateInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;amplitude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&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;lower&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;valor&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;amplitud&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;upper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;valor&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;amplitud&lt;/span&gt;
    &lt;span class="nf"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Using destructuring - Preferrable.&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generateInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;amplitude&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"[&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;lower&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;;&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;upper&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;]"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [8;12]&lt;/span&gt;

&lt;span class="c1"&gt;// Accessing by order to the anonymous components&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generateInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;amplitude&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"[&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;interval&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;;&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;interval&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;]"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [8;12]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Optional data types
&lt;/h1&gt;

&lt;p&gt;Let's suppose we want to describe a person. What attributes does a person have? We could say &lt;code&gt;firstName&lt;/code&gt;, &lt;code&gt;lastName&lt;/code&gt;, &lt;code&gt;identifier&lt;/code&gt;, &lt;code&gt;age&lt;/code&gt;, etc. All of them exist. A person always have a &lt;code&gt;firstName&lt;/code&gt;. A person always have &lt;code&gt;lastName&lt;/code&gt;. All of them are of type &lt;code&gt;String&lt;/code&gt;. &lt;code&gt;String&lt;/code&gt; is a &lt;strong&gt;non-optional type&lt;/strong&gt;.&lt;br&gt;
However, this isn't true for every attribute in a person. What about &lt;code&gt;middleName&lt;/code&gt;? A person may not have &lt;code&gt;middleName&lt;/code&gt;, it's &lt;strong&gt;OPTIONAL&lt;/strong&gt;.&lt;br&gt;
Optional data types in Swift let us design exactly that, values that may not be there, they may be &lt;code&gt;nil&lt;/code&gt;. You might have already noticed that in all examples so far, we have used non-optional values, and only in specific places we have used &lt;code&gt;!&lt;/code&gt; to note that we are sure that the value is not &lt;code&gt;nil&lt;/code&gt;, like in &lt;code&gt;first!&lt;/code&gt;, for &lt;code&gt;Array&lt;/code&gt;.&lt;br&gt;
Every data type have its optional counterpart, that is defined with the &lt;code&gt;?&lt;/code&gt; suffix. So, for instance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;String?&lt;/code&gt; is a &lt;code&gt;String&lt;/code&gt; that might have a &lt;code&gt;String&lt;/code&gt; value or &lt;code&gt;nil&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Int?&lt;/code&gt; is an &lt;code&gt;Int&lt;/code&gt; that might be &lt;code&gt;nil&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Bool?&lt;/code&gt; is a &lt;code&gt;Bool&lt;/code&gt; that might be &lt;code&gt;nil&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[String]?&lt;/code&gt; is an &lt;code&gt;Array&lt;/code&gt; of &lt;code&gt;String&lt;/code&gt; that might be &lt;code&gt;nil&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(quotient: Int, remainder: Int)?&lt;/code&gt; is a type composed by two &lt;code&gt;Int&lt;/code&gt; values. The tuple might be &lt;code&gt;nil&lt;/code&gt;. Its components, on the other hand, can't be &lt;code&gt;nil&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(firstName: String, lastName: String?)&lt;/code&gt; is a tuple composed by two &lt;code&gt;String&lt;/code&gt; values. The first one (&lt;code&gt;firstName&lt;/code&gt;) can't be &lt;code&gt;nil&lt;/code&gt;. The second one might be &lt;code&gt;nil&lt;/code&gt;. The &lt;code&gt;tuple&lt;/code&gt; isn't optional.
&lt;/li&gt;
&lt;/ul&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;PersonData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;firstName&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="nv"&gt;middleName&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="nv"&gt;lastName&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="nv"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&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;fernando&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;PersonData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Fernando"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;middleName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Martín"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Ortiz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;29&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;nicolas&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;PersonData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Nicolás"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;middleName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// it's nil, it doesn't exist&lt;/span&gt;
    &lt;span class="nv"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Duarte"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;29&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Force unwrap optionals
&lt;/h2&gt;

&lt;p&gt;If we're sure that an optional value isn't nil, we can force it to convert it into its non-optional counterpart. To do that, we can add a &lt;code&gt;!&lt;/code&gt; at the end of the variable name, and it will become non-optional.&lt;br&gt;
Let's see some examples. I have to say, however, that this is a &lt;strong&gt;VERY BAD PRACTICE&lt;/strong&gt;. If we add a &lt;code&gt;!&lt;/code&gt; to get the non-optional counterpart of a value that is actually &lt;code&gt;nil&lt;/code&gt;, the app will crash because of a fatal error.&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Fernando's middle name is &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;fernando&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;middleName&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Fernando's middle name is Optional("Martín")&lt;/span&gt;
&lt;span class="c1"&gt;// We have to get the non optional value of Fernando's middle name.&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;"Fernando's middle name is &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;fernando&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;middleName&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Fernando's middle name is Martín&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  if-let
&lt;/h2&gt;

&lt;p&gt;This is one of the "correct" ways of handling optional values. Inside an &lt;code&gt;if&lt;/code&gt;, we can include an assignment from an optional that will be executed just in the case that the value isn't &lt;code&gt;nil&lt;/code&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;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;middleName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fernando&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;middleName&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Inside here, middleName isn't optional&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;"Fernando's middle name is &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;middleName&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Fernando's middle name is Martín&lt;/span&gt;
&lt;span class="p"&gt;}&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;middleName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nicolas&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;middleName&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// This won't be executed&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;"Nicolas' middle name is &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;middleName&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&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;"Nicolas doesn't have a middle name"&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;h2&gt;
  
  
  guard-let
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;guard&lt;/code&gt; is the opposite of &lt;code&gt;if&lt;/code&gt;. It will execute its body only in the case that the condition isn't met and it's used to express a precondition. &lt;code&gt;guard&lt;/code&gt; can also be used to unwrap optionals in a safe way, such as &lt;code&gt;if-let&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;func&lt;/span&gt; &lt;span class="nf"&gt;getMiddleName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="nv"&gt;person&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;PersonData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;middleName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;middleName&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; doesn't have middle name"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;'s middle name is &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;middleName&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&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="nf"&gt;getMiddleName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;for&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;fernando&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// Fernando's middle name is Martín&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getMiddleName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;nicolas&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// Nicolás doesn't have middle name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Implicitly unwrapped optionals
&lt;/h2&gt;

&lt;p&gt;An implicitly unwrapped optional is an optional data type, that we can assign &lt;code&gt;nil&lt;/code&gt; to, obviously, but that when we need to use it, it will be considered non optional for most of the cases.&lt;br&gt;
Of course, the app will crash in case the value is actually &lt;code&gt;nil&lt;/code&gt; and we try to use it as a non-nil value. And, of course, this is a bad practice. However, this is used a lot in iOS, especially in frameworks that are written in Objective-C (Swift's predecessor, in Apple's platforms).&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;let&lt;/span&gt; &lt;span class="nv"&gt;nonNilMiddleName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Marcos"&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;nilMiddleName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;printMiddleName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;middleName&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;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;middleName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;printMiddleName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonNilMiddleName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Marcos&lt;/span&gt;
&lt;span class="c1"&gt;// printMiddleName(nilMiddleName) // CRASH!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Optional chaining
&lt;/h2&gt;

&lt;p&gt;Let's suppose we have an optional tuple.&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;optionalPerson&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;PersonData&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;
&lt;span class="n"&gt;optionalPerson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nv"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Nicolás"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;middleName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Duarte"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nv"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;29&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If in this case, that &lt;code&gt;optionalPerson&lt;/code&gt; is optional, we need to access any of its components, being them attributes or methods (such as in classes, as we'll see later), we can use a feature called &lt;strong&gt;Optional chaining&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Optional chaining lets us access members of the optional object using &lt;code&gt;?.&lt;/code&gt; instead of &lt;code&gt;.&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="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;optionalPerson&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="c1"&gt;// type: String?, because, although `firstName` in `PersonData` is `String`, we don't know if `optionalPerson` exists, or if it's `nil`.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Exercises
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Define a tuple that describes an address, with fields like &lt;code&gt;city&lt;/code&gt;, &lt;code&gt;state&lt;/code&gt;, &lt;code&gt;zipCode&lt;/code&gt;, &lt;code&gt;country&lt;/code&gt;, etc. Feel free to experiment and use dictionaries, optional values, and everything we've learned in this lesson.&lt;/li&gt;
&lt;li&gt;Inside the address, define some optional components, like &lt;code&gt;floorNumber&lt;/code&gt; and &lt;code&gt;apartment&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Create three addresses as constants.&lt;/li&gt;
&lt;li&gt;Write a function that gets an address and prints it as a well formatted &lt;code&gt;String&lt;/code&gt;. Use interpolation.&lt;/li&gt;
&lt;li&gt;Write a function that gets an &lt;code&gt;Array&lt;/code&gt; of addresses and returns an &lt;code&gt;String&lt;/code&gt; with &lt;code&gt;"floor: \(floor) ; apt: \(apartment)"&lt;/code&gt; ONLY for those addresses with non-nil floor and apartment.&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>Swift generics</title>
      <dc:creator>Fernando Martín Ortiz</dc:creator>
      <pubDate>Sun, 25 Apr 2021 22:59:30 +0000</pubDate>
      <link>https://dev.to/fmo91/swift-generics-27o9</link>
      <guid>https://dev.to/fmo91/swift-generics-27o9</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This article is part of the course&lt;/em&gt; &lt;strong&gt;Introduction to iOS using UIKit&lt;/strong&gt; &lt;em&gt;I've given many times in the past. The course was originally in Spanish, and I decided to release it in English so more people can read it and hopefully it will help them.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  The problem
&lt;/h1&gt;

&lt;p&gt;Let's imagine we need to write a &lt;code&gt;Stack&lt;/code&gt; data structure. We could do something like this:&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;Stack&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;topElement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;mutating&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;mutating&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popLast&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;p&gt;And we can use it like this:&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;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Stack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;topElement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 20&lt;/span&gt;
&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&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;topElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem we'll have with this type is that if we need to define a &lt;code&gt;StringStack&lt;/code&gt;, then we'd have to repeat the entire code!&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;StringStack&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;content&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;topElement&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;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;mutating&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;element&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;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;mutating&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;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;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popLast&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;h1&gt;
  
  
  The solution
&lt;/h1&gt;

&lt;p&gt;The solution to that problem is &lt;strong&gt;generics&lt;/strong&gt;, and it consists on replacing the specific types that will vary from implementation to implementation, by a template or generic type.&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;Stack&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;topElement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;mutating&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;mutating&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popLast&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;p&gt;We don't have a &lt;code&gt;IntStack&lt;/code&gt; and a &lt;code&gt;StringStack&lt;/code&gt; as separate types anymore. What we have now is a &lt;code&gt;Stack&lt;/code&gt; of type &lt;code&gt;T&lt;/code&gt;, and that &lt;code&gt;T&lt;/code&gt; can be replaced by a concrete type when we need a &lt;code&gt;Stack&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Stack&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;topElement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 20&lt;/span&gt;
&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&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;topElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Constrained generics
&lt;/h1&gt;

&lt;p&gt;Sometimes we need to force generic types to implement certain requisites. The most common case is that we need the generic type to implement a protocol.&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;protocol&lt;/span&gt; &lt;span class="kt"&gt;Noisy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;makeNoise&lt;/span&gt;&lt;span class="p"&gt;()&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;RingBell&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Noisy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;makeNoise&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="s"&gt;"Ring!"&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="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Noisy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;makeNoise&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="s"&gt;"Woof!"&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;p&gt;Let's now imagine we want to create a noisy stack, so we can then have all our noisy objects to make noise at the same 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="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;NoisyStack&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Noisy&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;T&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="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;topElement&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;mutating&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;mutating&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;T&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;popLast&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;makeNoise&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;makeNoise&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;p&gt;In this case, as the items in the &lt;code&gt;NoisyStack&lt;/code&gt; are &lt;code&gt;Noisy&lt;/code&gt;, we know that all the items on can make noise.&lt;/p&gt;

&lt;h1&gt;
  
  
  Examples
&lt;/h1&gt;

&lt;p&gt;There are other examples of generic types we have been using so far.&lt;/p&gt;

&lt;p&gt;The first one is &lt;code&gt;Array&lt;/code&gt;. When we write &lt;code&gt;[Int]&lt;/code&gt;, we're actually creating a &lt;code&gt;Array&amp;lt;Int&amp;gt;&lt;/code&gt;, but with a simplified syntax.&lt;/p&gt;

&lt;p&gt;The second example is probably more interesting. &lt;code&gt;Int?&lt;/code&gt; is actually &lt;code&gt;Optional&amp;lt;Int&amp;gt;&lt;/code&gt;, but with some syntax sugar on it, but it's exactly the same as writing something like this:&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;enum&lt;/span&gt; &lt;span class="kt"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Wrapped&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;none&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Wrapped&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;



</description>
    </item>
    <item>
      <title>Introduction to Swift error handling</title>
      <dc:creator>Fernando Martín Ortiz</dc:creator>
      <pubDate>Sun, 25 Apr 2021 22:28:55 +0000</pubDate>
      <link>https://dev.to/fmo91/introduction-to-swift-error-handling-45p8</link>
      <guid>https://dev.to/fmo91/introduction-to-swift-error-handling-45p8</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This article is part of the course&lt;/em&gt; &lt;strong&gt;Introduction to iOS using UIKit&lt;/strong&gt; &lt;em&gt;I've given many times in the past. The course was originally in Spanish, and I decided to release it in English so more people can read it and hopefully it will help them.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Swift.Error
&lt;/h1&gt;

&lt;p&gt;So far, we've been using optionals and other techniques to represent errors in Swift.&lt;/p&gt;

&lt;p&gt;However, Swift has a native, correct way of representing errors, and that way is using custom enum types implementing &lt;code&gt;Swift.Error&lt;/code&gt;, or simply &lt;code&gt;Error&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Error&lt;/code&gt; protocol won't ask us to implement any method or property.&lt;/p&gt;

&lt;p&gt;Let's suppose we are modeling a banking app, so we could define our errors with a enum like this, where each of its cases will represent a different error, and the enum itself will implement the &lt;code&gt;Error&lt;/code&gt; protocol.&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;enum&lt;/span&gt; &lt;span class="kt"&gt;AccountError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;insufficientFunds&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;wrongPinCode&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;wrongAlphabeticCode&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  throws, throw
&lt;/h1&gt;

&lt;p&gt;Once we've defined our &lt;code&gt;Error&lt;/code&gt; enum, we can implement functions that use those errors. However, in order to do that, we must specify that our function can throw an error. This is done with the &lt;code&gt;throws&lt;/code&gt; keyword.&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;class&lt;/span&gt; &lt;span class="kt"&gt;Account&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;funds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;funds&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&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;p&gt;In this case, the &lt;code&gt;extract&lt;/code&gt; method could throw an error, if we try to extract an amount we don't have in the account.&lt;/p&gt;

&lt;p&gt;Let's then check that using a &lt;code&gt;guard&lt;/code&gt; clause. If we find an error, we need to &lt;code&gt;throw&lt;/code&gt; it specifying which error it is.&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;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="n"&gt;funds&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="kt"&gt;AccountError&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;insufficientFunds&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;funds&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  do/try/catch
&lt;/h1&gt;

&lt;p&gt;Nice! We can throw errors. Our next challenge is to handle them effectively when we get them.&lt;/p&gt;

&lt;p&gt;The first we have to notice is that throwing functions need to be handled in a special way to use them.&lt;/p&gt;

&lt;p&gt;Let's start with the code and then we'll explain 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;let&lt;/span&gt; &lt;span class="nv"&gt;account&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="kt"&gt;AccountError&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;insufficientFunds&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;"Insufficient funds!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;default&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;"Another error."&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;p&gt;In the previous example we've defined two different regions.&lt;/p&gt;

&lt;p&gt;One, inside the &lt;code&gt;do&lt;/code&gt; block, where we can call throwing functions. Whenever we need to call a function that may throw, we use the &lt;code&gt;try&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;The second region is defined inside the &lt;code&gt;catch&lt;/code&gt; block. Inside the &lt;code&gt;catch&lt;/code&gt; block we can define a custom name for our error. This is done with a &lt;code&gt;let&lt;/code&gt; binding, followed by the error name. In this case, its called &lt;code&gt;error&lt;/code&gt;, which is also the default error name, so it's unneeded in this case.&lt;/p&gt;

&lt;p&gt;Inside the &lt;code&gt;catch&lt;/code&gt; block, we can receive our error, which will be of type &lt;code&gt;Error&lt;/code&gt;, so we can switch on it and do something different for each error case we've defined, and a &lt;code&gt;default&lt;/code&gt; clause in case we are getting an error we aren't handling specifically.&lt;/p&gt;

&lt;h1&gt;
  
  
  try, try?, try!
&lt;/h1&gt;

&lt;p&gt;Let's modify a bit our function so it will return an object of type &lt;code&gt;Extraction&lt;/code&gt;, which will represent the extraction we're performing.&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;Extraction&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;id&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;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;uuidString&lt;/span&gt; &lt;span class="c1"&gt;// Random ID string&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;amount&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;And for our &lt;code&gt;extract&lt;/code&gt; method:&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;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Extraction&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="n"&gt;funds&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="kt"&gt;AccountError&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;insufficientFunds&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;funds&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;Extraction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;amount&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;Let's suppose we are only interested in getting our &lt;code&gt;Extraction&lt;/code&gt; object, and we aren't interested in the specific error we might get. We can get the &lt;code&gt;Extraction&lt;/code&gt; object directy using any of these two &lt;code&gt;try&lt;/code&gt; variations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;try?&lt;/strong&gt;: Returns the throwing function result as an Optional value, which is &lt;code&gt;nil&lt;/code&gt; in case the function throws.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;try!&lt;/strong&gt;: Returns the throwing function result as a non-optional value, or crashes in case the function throws.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, we must be very cautious when using &lt;code&gt;try!&lt;/code&gt;. &lt;code&gt;try?&lt;/code&gt; instead, is much more widely used.&lt;/p&gt;

&lt;p&gt;Let's see the three cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  try:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;account&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;do&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;extraction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// `extraction` is non-optional.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="kt"&gt;AccountError&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;insufficientFunds&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;"Insufficient funds"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;default&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;"Another error"&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;h2&gt;
  
  
  try?
&lt;/h2&gt;

&lt;p&gt;In this case, &lt;code&gt;extraction&lt;/code&gt; is of type &lt;code&gt;Extraction?&lt;/code&gt;, optional, and doesn't require a &lt;code&gt;do-catch&lt;/code&gt; block.&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;let&lt;/span&gt; &lt;span class="nv"&gt;extraction&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="n"&gt;account&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  try!
&lt;/h2&gt;

&lt;p&gt;In this case, &lt;code&gt;extraction&lt;/code&gt; is of type &lt;code&gt;Extraction&lt;/code&gt;, non-optional, and doesn't require a &lt;code&gt;do-catch&lt;/code&gt; block.&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;let&lt;/span&gt; &lt;span class="nv"&gt;extraction&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="n"&gt;account&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Result
&lt;/h1&gt;

&lt;p&gt;Another way of working with errors is by using the &lt;code&gt;Result&lt;/code&gt; type, included in the standard Swift library.&lt;/p&gt;

&lt;p&gt;Remember that an enum could have values associated to each of its cases. Also, an enum can be generic!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Result&lt;/code&gt; is a generic type with two cases with associated values. The &lt;code&gt;Result&lt;/code&gt; type is defined &lt;em&gt;something like&lt;/em&gt; this, and has many methods to work with these two possibilities:&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;enum&lt;/span&gt; &lt;span class="kt"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;SuccessType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;ErrorType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nf"&gt;success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;SuccessType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nf"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;ErrorType&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;This means that &lt;code&gt;Result&lt;/code&gt; is a type that represents a case in which the operation has been successful, and a second case in which the operation has failed, so we will get the &lt;code&gt;Error&lt;/code&gt; for that failure.&lt;/p&gt;

&lt;p&gt;Of course, &lt;code&gt;Result&lt;/code&gt; is included in the standard Swift library, so we won't need to define our own &lt;code&gt;Result&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;Let's rewrite the &lt;code&gt;extract&lt;/code&gt; function so it'll return &lt;code&gt;Result&amp;lt;Extraction, AccountError&amp;gt;&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;func&lt;/span&gt; &lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Extraction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;AccountError&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="n"&gt;funds&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Extraction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;AccountError&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="nf"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;AccountError&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;insufficientFunds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;funds&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;extraction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Extraction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Extraction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;AccountError&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="nf"&gt;success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;extraction&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;As it's obvious that the result is of type &lt;code&gt;Result&amp;lt;Extraction, AccountError&amp;gt;&lt;/code&gt;, so we can avoid writing that type in the &lt;code&gt;return&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;func&lt;/span&gt; &lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Extraction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;AccountError&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="n"&gt;funds&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;AccountError&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;insufficientFunds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;funds&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;extraction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Extraction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;extraction&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;Once our function is rewritten this way, we can modify the code that calls the &lt;code&gt;extract&lt;/code&gt; function.&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;let&lt;/span&gt; &lt;span class="nv"&gt;extractionResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;extractionResult&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;success&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;extraction&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;"Extraction id: &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;extraction&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;failure&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;error&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;insufficientFunds&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;"Insufficient funds"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;wrongAlphabeticCode&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;"Wrong alphabetic code"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;wrongPinCode&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;"Wrong pin code"&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;



</description>
    </item>
    <item>
      <title>Introduction to the Controller</title>
      <dc:creator>Fernando Martín Ortiz</dc:creator>
      <pubDate>Sun, 25 Apr 2021 19:01:25 +0000</pubDate>
      <link>https://dev.to/fmo91/introduction-to-the-controller-5422</link>
      <guid>https://dev.to/fmo91/introduction-to-the-controller-5422</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This article is part of the course&lt;/em&gt; &lt;strong&gt;Introduction to iOS using UIKit&lt;/strong&gt; &lt;em&gt;I've given many times in the past. The course was originally in Spanish, and I decided to release it in English so more people can read it and hopefully it will help them.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Views in UIKit
&lt;/h1&gt;

&lt;p&gt;Views in UIKit inherit from the base class &lt;strong&gt;UIView&lt;/strong&gt;. There are specialized type of views, such a buttons, sliders, lists, grids, etc. We can use UIView as it is, or subclass it, or use its subclasses.&lt;/p&gt;

&lt;h2&gt;
  
  
  UILabel
&lt;/h2&gt;

&lt;p&gt;It's a text label. &lt;/p&gt;

&lt;p&gt;Some of its more frequently used members:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;text: String&lt;/strong&gt; - it defines the text the label shows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;textColor: UIColor&lt;/strong&gt; - it defines the text color of the label.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;font: UIFont&lt;/strong&gt; - it defines the font the label uses.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  UIButton
&lt;/h2&gt;

&lt;p&gt;It's a button with a text label on it. We can customize its behavior when it's clicked (more on this later).&lt;/p&gt;

&lt;p&gt;Some of its more frequently used members:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;setTitle(_:for:)&lt;/strong&gt; - modifies the text inside the button label.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;setTitleColor(_:for:)&lt;/strong&gt; - modifies the text color of the button label.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For both of them, in the &lt;strong&gt;for&lt;/strong&gt; param, let's use &lt;strong&gt;.normal&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  UIImageView
&lt;/h2&gt;

&lt;p&gt;It's a view with a image on it.&lt;/p&gt;

&lt;p&gt;Some of its more frequently used members:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;image: UIImage&lt;/strong&gt; - defines the image the view is showing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;contentMode: ContentMode&lt;/strong&gt; - defines the way the image will fit into this view in case their sizes aren't equal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most used values of &lt;strong&gt;ContentMode&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;ContentMode.scaleToFit&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdrtpi1ou7sxgty7utjpr.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdrtpi1ou7sxgty7utjpr.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ContentMode.scaleToFill&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft6i7nx4bp9edh0vi2cy2.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft6i7nx4bp9edh0vi2cy2.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ContentMode.scaleAspectFit&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmwdhumtpl44k7429lkz.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmwdhumtpl44k7429lkz.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  UITextField
&lt;/h2&gt;

&lt;p&gt;It's a view that shows an editable one-line text.&lt;/p&gt;

&lt;p&gt;Some of its more frequently used members:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;text: String&lt;/strong&gt; - defines the text the textField shows.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;textColor: UIColor&lt;/strong&gt; - defines the textField text color.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;keyboardType: UIKeyboardType&lt;/strong&gt; - defines the keyboard type that will be displayed while interacting with the text field.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;isSecureTextEntry: Bool&lt;/strong&gt; - defines whether to show or not the text field as a password field with dots instead of characters.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  UIStackView
&lt;/h2&gt;

&lt;p&gt;It's a view that displays other views inside of it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It allow us to sort view in vertical or horizontal axis.&lt;/li&gt;
&lt;li&gt;A stackView doesn't require constraints for its subviews, although it needs constraints to display itself inside its container.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  UIViewController life cycle
&lt;/h1&gt;

&lt;h2&gt;
  
  
  What is a life cycle?
&lt;/h2&gt;

&lt;p&gt;A lifecycle is the set of states that define an object from its creation to its destruction.&lt;/p&gt;

&lt;p&gt;In case of a controller or view, we aren't allows to modify its life cycle state, but &lt;strong&gt;we can react to those changes&lt;/strong&gt;, and customize what happens when a view or a controller changes its state.&lt;/p&gt;

&lt;p&gt;Whenever an object changes its state, a life cycle method (or simply called hook) is executed. We can &lt;strong&gt;override&lt;/strong&gt; any of those methods to customize what will happen in such a case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fab1hh6wdr7vueqdorzkh.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fab1hh6wdr7vueqdorzkh.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The diagram above shows the different methods in the view controller life cycle and the possible transitions between them. &lt;/p&gt;

&lt;p&gt;For this course, we'll just see the most important states and transitions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;init&lt;/strong&gt;: it's executed when a controller is instantiated. In this moment, we don't have the view loaded in memory yet. If we want to access an element in the view at this point, the app will crash. Be careful.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;viewDidLoad&lt;/strong&gt;: Once the view in the controller has been loaded into memory, this method will be executed. It will happen once in the entire controller's life cycle.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;viewWillAppear&lt;/strong&gt;: Once the view in the controller is shown to the user, this method will be executed. It can happen any number of times during the controller's life cycle. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;viewDidAppear&lt;/strong&gt;: This will be executed once the view is already shown in screen. If the app navigates to another view using an animated transition, this method will be executed once the transition has finished animating. &lt;strong&gt;In this moment, the views has actual values for its constraints.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;viewWillDisappear&lt;/strong&gt;/&lt;strong&gt;viewDidDisappear&lt;/strong&gt;: They are like &lt;strong&gt;viewWillAppear&lt;/strong&gt; and &lt;strong&gt;viewDidAppear&lt;/strong&gt; but in the opposite direction.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  @IBOutlet and @IBAction
&lt;/h1&gt;

&lt;p&gt;A controller has two responsibilities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;To send the data from the model to the view&lt;/strong&gt;: This can be done by setting attributes to the views the controller has access to. The controller must have references to its views. Those references are simple implicitly unwrapped optional (!) variables marked with &lt;strong&gt;@IBOutlet&lt;/strong&gt;. So, @IBOutlet means that the view annotated with it is going to be linked to a view in the interface builder.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;To reflect user actions in the model&lt;/strong&gt;: This is done by having methods in the controller annotated with &lt;strong&gt;@IBAction&lt;/strong&gt;, which will be linked to actions in views (such as a button tap). There are other ways other than using @IBAction, but that's good enough to start.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's say we have this view:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw70qwe478bzt67yqwloz.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw70qwe478bzt67yqwloz.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The functionality that we need to add to the controller is to respond to the tap/click event in the &lt;strong&gt;UIButton&lt;/strong&gt; that says "Generate full name", so we can take the &lt;strong&gt;text&lt;/strong&gt; property in the &lt;strong&gt;UITextField&lt;/strong&gt; views for the first and last names, and replace the &lt;strong&gt;text&lt;/strong&gt; in the &lt;strong&gt;UILabel&lt;/strong&gt; that says "Full name".&lt;/p&gt;

&lt;p&gt;In order to do this, we need references in the controller for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;firstNameTextField: &lt;strong&gt;UITextField!&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;lastNameTextField: &lt;strong&gt;UITextField!&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;resultLabel: &lt;strong&gt;UILabel!&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why are these &lt;strong&gt;Optional&lt;/strong&gt;? Because when we instantiate the controller, the view isn't loaded yet! we know when the views are instantiated because the &lt;strong&gt;viewDidLoad&lt;/strong&gt; method is executed. This is &lt;strong&gt;VERY&lt;/strong&gt; important, it will save us from getting random crashes.&lt;/p&gt;

&lt;p&gt;To generate an &lt;strong&gt;@IBOutlet&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open an editor in the right&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmjn8ok0woudfvarh9vra.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmjn8ok0woudfvarh9vra.png" alt="Screen Shot 2021-04-25 at 15.46.04"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the right editor, open the class &lt;strong&gt;ViewController.swift&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F87qxtafkxhnhiczgbcvl.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F87qxtafkxhnhiczgbcvl.png" alt="Screen Shot 2021-04-25 at 15.46.47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the left editor, search for the view you want to link.&lt;/li&gt;
&lt;li&gt;Keeping pressed the &lt;strong&gt;control&lt;/strong&gt; key in the keyboard, drag and drop the view to any place of the controller code (not inside a method).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F62cj9gdy7p7k9vyvnxwa.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F62cj9gdy7p7k9vyvnxwa.png" alt="Screen Shot 2021-04-25 at 15.47.25"&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F83ghh98kfh4dxahyfez3.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F83ghh98kfh4dxahyfez3.png" alt="Screen Shot 2021-04-25 at 15.48.45"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A simple way of ensuring this has worked correctly is to select the view controller in the left editor. In the last tab in the right panel we'll see the names of the &lt;strong&gt;@IBOutlet&lt;/strong&gt; and &lt;strong&gt;@IBAction&lt;/strong&gt; we've linked.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh675vlhs2j5ot8dafy0j.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh675vlhs2j5ot8dafy0j.png" alt="Screen Shot 2021-04-25 at 15.51.38"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Apart from the &lt;strong&gt;@IBOutlet&lt;/strong&gt;, we'll need to react to the action in the &lt;strong&gt;UIButton&lt;/strong&gt; using an &lt;strong&gt;@IBAction&lt;/strong&gt;, that, as we've already learned, it's a method that will be executed when some action occurs in the 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;@IBAction&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;generateNameButtonPressed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;@IBAction&lt;/strong&gt; for a button might be associated to different events of that button. In general, we'll use the event &lt;strong&gt;touchUpInside&lt;/strong&gt;, which is the most standard tap on the button.&lt;/p&gt;

&lt;p&gt;The steps to generate a &lt;strong&gt;@IBAction&lt;/strong&gt; are exactly the same than for generating a &lt;strong&gt;@IBOutlet&lt;/strong&gt;, except that at the end, we need to choose an action instead of an outlet in the last step.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzyaszs6736v7z6t8fn6n.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzyaszs6736v7z6t8fn6n.png" alt="image"&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr5i9vxlqlf1sgys1u1ak.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr5i9vxlqlf1sgys1u1ak.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once views and events are connected, we can insert custom logic to the controller. In this case, we'll need to obtain the first name and last name from the &lt;strong&gt;UITextField&lt;/strong&gt; views, and then assign the concatenation of them to the text property in the &lt;strong&gt;UILabel&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff3tpc9wp06tbda9iocg3.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff3tpc9wp06tbda9iocg3.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Practice
&lt;/h1&gt;

&lt;p&gt;Create an app that is just a label with a &lt;strong&gt;0&lt;/strong&gt; (zero) in the middle of the screen with two buttons, one at the left for decrementing the value of the counter label, and another at the right for incrementing the value.&lt;/p&gt;

&lt;p&gt;Also... what if we add a textField to customize the amount we will increment or decrement when the buttons are pressed?&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Introduction to iOS using UIKit</title>
      <dc:creator>Fernando Martín Ortiz</dc:creator>
      <pubDate>Sun, 25 Apr 2021 18:36:07 +0000</pubDate>
      <link>https://dev.to/fmo91/free-ios-tutorial-with-uikit-nmm</link>
      <guid>https://dev.to/fmo91/free-ios-tutorial-with-uikit-nmm</guid>
      <description>&lt;p&gt;Today, I decided to start open sourcing my iOS tutorial I've been using to teach iOS trainees (50+ I think) over many years. The tutorial I'm going to be releasing is just the last of many iterations.&lt;/p&gt;

&lt;p&gt;I haven't done this before, because the course has been originally written in Spanish. This list of articles has been translated to English by myself.&lt;/p&gt;

&lt;p&gt;Before listing the articles, I have to say that, if you're starting iOS development in 2021, you probably want to start by learning SwiftUI, and not UIKit. When I started writing these tutorials (back in 2017), there was no clue we were going to have a new framework to nearly replace UIKit. &lt;/p&gt;

&lt;p&gt;If you prefer to learn SwiftUI, I recommend &lt;a href="https://www.hackingwithswift.com/100/swiftui"&gt;this course&lt;/a&gt; by Paul Hudson.&lt;/p&gt;

&lt;p&gt;If you're still here, this is the list of tutorials:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/fmo91/introduction-to-swift-part-1-10pc"&gt;Introduction to Swift - Part 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/fmo91/introduction-to-swift-part-2-7m0"&gt;Introduction to Swift - Part 2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/fmo91/introduction-to-the-view-3a8j"&gt;Introduction to the View&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/fmo91/introduction-to-the-controller-5422"&gt;Introduction to the Controller&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/fmo91/using-xibs-2ime"&gt;Using Xibs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/fmo91/navigation-in-ios-apps-4jm3"&gt;Navigation in iOS apps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Understanding Delegation &lt;em&gt;(under translation)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Introduction to UITableView &lt;em&gt;(under translation)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Introduction to Networking &lt;em&gt;(under translation)&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Extra:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Introduction to Cocoapods &lt;em&gt;(under translation)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/fmo91/swift-generics-27o9"&gt;Swift generics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Introduction to Git &lt;em&gt;(under translation)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/fmo91/introduction-to-swift-error-handling-45p8"&gt;Introduction to Swift error handling&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;--&lt;/p&gt;

&lt;p&gt;I honestly hope anyone could take advantage of these course.&lt;/p&gt;

&lt;p&gt;Please reach me over dev.to, or to my &lt;a href="//mailto:ortizfernandomartin@gmail.com"&gt;email&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Introduction to the View</title>
      <dc:creator>Fernando Martín Ortiz</dc:creator>
      <pubDate>Sun, 25 Apr 2021 16:53:40 +0000</pubDate>
      <link>https://dev.to/fmo91/introduction-to-the-view-3a8j</link>
      <guid>https://dev.to/fmo91/introduction-to-the-view-3a8j</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This article is part of the course&lt;/em&gt; &lt;strong&gt;Introduction to iOS using UIKit&lt;/strong&gt; &lt;em&gt;I've given many times in the past. The course was originally in Spanish, and I decided to release it in English so more people can read it and hopefully it will help them.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  SwiftUI? UIKit?
&lt;/h1&gt;

&lt;p&gt;When we develop apps for iOS, we have two base frameworks we can choose from. &lt;strong&gt;SwiftUI&lt;/strong&gt; and &lt;strong&gt;UIKit&lt;/strong&gt;.&lt;br&gt;
Both of them allow us to develop user interfaces (UIs).&lt;/p&gt;

&lt;h2&gt;
  
  
  SwiftUI
&lt;/h2&gt;

&lt;p&gt;Released in 2019. It's a framework for building &lt;strong&gt;declarative user interfaces&lt;/strong&gt;. It's modern and its approach is similar to React, Flutter and Jetpack Compose in other platforms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;It's clearly the future of iOS development.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You might be forced to use UIKit, because of backwards compatibility, or because it's the framework used in the company you work for. If not, I recommend closing this course and start learning SwiftUI using some the great tutorials in the web, like Paul Hudson's &lt;a href="https://www.hackingwithswift.com/100/swiftui"&gt;100 days of SwiftUI&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  UIKit
&lt;/h2&gt;

&lt;p&gt;UIKit is the framework all iOS user interfaces are based upon. SwiftUI uses many UIKit components under the hood when compiling for iOS, so knowing UIKit is a great knowledge to have, even if you aren't directly using it in a daily basis.&lt;/p&gt;

&lt;p&gt;Another important thing is that SwiftUI can be extended using UIKit components. And you can embed SwiftUI in UIKit if needed. So both of them are complementary to some extent.&lt;/p&gt;

&lt;p&gt;In this course we'll focus on UIKit, using MVC as the design pattern for organizing our code.&lt;/p&gt;

&lt;h1&gt;
  
  
  Model - View - Controller
&lt;/h1&gt;

&lt;p&gt;Design patterns are solutions for software design problems. If many people experience the same problems over and over again, it makes sense to document the ways that problem can be solved, regardless of the specific language or platform you're working on. &lt;/p&gt;

&lt;p&gt;Model View Controller (MVC) is the design pattern Apple recommends to use in order to design iOS applications.&lt;/p&gt;

&lt;p&gt;The MVC pattern divides classes in three different groups or layers:&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;Model&lt;/strong&gt; we have all the classes that are responsible for managing the data for your app. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_T37owAy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0tcfcliajxxlobybh1ic.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_T37owAy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0tcfcliajxxlobybh1ic.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;View&lt;/strong&gt; we include all the classes that are responsible for showing the data (or anything needed) to your users.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VVMb6uqn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1lc6phz37ilq7scbg6lo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VVMb6uqn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1lc6phz37ilq7scbg6lo.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So we have a model that holds our data and a view that shows that data to our users.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_n-OaZMe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hg77zppd8707l16xs8hx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_n-OaZMe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hg77zppd8707l16xs8hx.png" alt="Tutos-Page-2"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;So, the question here becomes: how does the view know the model and what subset of the model data it should be showing to the user? &lt;/p&gt;

&lt;p&gt;As iOS developers, we are developers of user interfaces. The goal of any user interface is to provide a consistent representation of the internal state of a system in a visual way to a user.&lt;br&gt;
The internal state is known as a &lt;strong&gt;model&lt;/strong&gt;, and can be as simple as plain Swift classes.&lt;br&gt;
The visual representation is known as &lt;strong&gt;view&lt;/strong&gt; and we'll develop it using components defined in UIKit.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zuCsNR-v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cldn2ahrx6x40augmybv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zuCsNR-v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cldn2ahrx6x40augmybv.png" alt="Tutos-01"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As user interfaces developers we have two goals:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, we must &lt;strong&gt;send the data from the model to the view&lt;/strong&gt;. The view must always reflect the current model state.&lt;/li&gt;
&lt;li&gt;Second, we must reflect the user actions in model data changes. We have to modify the data in the model according to what happens in the user interface.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both of those actions are done in the &lt;strong&gt;controller&lt;/strong&gt;. At least at the beginning, you can assume that every screen is controlled by a controller.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Hz3GG-wU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6vda8yspmjdcj0kesmv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Hz3GG-wU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6vda8yspmjdcj0kesmv.png" alt="Tutos-Page-3"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Each &lt;strong&gt;view&lt;/strong&gt; is a subclass of &lt;strong&gt;UIView&lt;/strong&gt;, defined in UIKit.&lt;br&gt;
Each &lt;strong&gt;controller&lt;/strong&gt; is a subclass of &lt;strong&gt;UIViewController&lt;/strong&gt;, defined in UIKit.&lt;br&gt;
Each &lt;strong&gt;controller&lt;/strong&gt; has a &lt;strong&gt;root view&lt;/strong&gt;. That root view can be accessed through the &lt;strong&gt;.view&lt;/strong&gt; property in &lt;strong&gt;UIViewController&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  The View
&lt;/h1&gt;

&lt;p&gt;The view is the layer responsible of visually representing the state of the model to a user, and it's organized in the form of a &lt;strong&gt;tree&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Quick recap for those who don't know/remember what a tree is: A tree is a hierarchical data structure composed by nodes. Starting from a root node, each node may have children, which may also have other children node, until we finally get nodes without children. Those nodes are known as leaves.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The view is organized as a tree. That means that we will have a root view, which can be accessed from our view controller. Starting from our root view, we may find children views, or subviews, which may have their own subviews.&lt;/p&gt;

&lt;p&gt;So, for this example:&lt;/p&gt;

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

&lt;p&gt;This could be a possible (simplified) view tree (or view hierarchy):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0C6yN843--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/by3xxlphnwslgox48x7c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0C6yN843--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/by3xxlphnwslgox48x7c.png" alt="Tutos-Page-4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Constraints
&lt;/h1&gt;

&lt;p&gt;Given that the views are the elements shown to the user, and that they compose in form of tree structures, the constraints are the elements that will let us define the exact place where each view should be shown in the screen.&lt;/p&gt;

&lt;p&gt;In order to place a view in screen, we must define two values: &lt;strong&gt;origin&lt;/strong&gt; and &lt;strong&gt;size&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;origin&lt;/strong&gt; is the point from where the view starts to be drawn. This value is expressed as a coordinate (CGPoint to be precise), defining the top left corner of the view.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IjEgQnFW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0nmywflpl2u5donntpv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IjEgQnFW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e0nmywflpl2u5donntpv.png" alt="Tutos-Page-5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While the size is a value composed of the width and size of the view (CGSize to be precise).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Eigfx-OU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wif4u5nfsbu4slhl6cz7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Eigfx-OU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wif4u5nfsbu4slhl6cz7.png" alt="Tutos-Page-6"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Constraints are just that, constraints, that we apply to our views. The requisite is that we need to determine the origin and size of our views in such a way that isn't ambiguous to UIKit.&lt;br&gt;
If after we apply our constraints to our views, they can take more than one possible value for any of those dimensions (origin.x, origin.y, size.width, size.height), then the constraints applied to the view are considered &lt;strong&gt;ambiguous&lt;/strong&gt;, and we can get non expected results.&lt;br&gt;
&lt;strong&gt;We must be specific.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A constraint is composed by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;origin view + origin attribute&lt;/li&gt;
&lt;li&gt;destination view + destination attribute&lt;/li&gt;
&lt;li&gt;constant&lt;/li&gt;
&lt;li&gt;multiplier (by default it's 1.0)&lt;/li&gt;
&lt;li&gt;relationship operator (by default, and almost always, it's going to be ==)&lt;/li&gt;
&lt;li&gt;priority (by default, 1000 (required)).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Based on these elements, we can build EVERY POSSIBLE LAYOUT. That's why the UIKit constraints system is so powerful.&lt;/p&gt;

&lt;p&gt;Attributes can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;leading&lt;/li&gt;
&lt;li&gt;trailing&lt;/li&gt;
&lt;li&gt;top&lt;/li&gt;
&lt;li&gt;bottom&lt;/li&gt;
&lt;li&gt;width&lt;/li&gt;
&lt;li&gt;height&lt;/li&gt;
&lt;li&gt;center X&lt;/li&gt;
&lt;li&gt;center Y&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example 1
&lt;/h2&gt;

&lt;p&gt;We want to add a view to a container in such a way te view will occupy the total space of its container.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z6Vi2iOe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zpyz3yyzif9k3dxog9di.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z6Vi2iOe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zpyz3yyzif9k3dxog9di.png" alt="Tutos-Page-7"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;View2.top == View1.top (constant: 0)&lt;/li&gt;
&lt;li&gt;View2.trailing == View1.trailing (constant: 0)&lt;/li&gt;
&lt;li&gt;View2.bottom == View1.bottom (constant: 0)&lt;/li&gt;
&lt;li&gt;View2.leading == View1.leading (constant: 0)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is other way for them to follow these constraints than to be equal in terms of origin and size. And that's the way to think on constraints.&lt;br&gt;
We left them no choice. The only thing they can do is to fit in the place we want.&lt;/p&gt;

&lt;p&gt;Alternative solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;View2.width == View1.width (constant: 0)&lt;/li&gt;
&lt;li&gt;View2.height == View1.height (constant: 0)&lt;/li&gt;
&lt;li&gt;View2.centerX == View1.centerX (constant: 0)&lt;/li&gt;
&lt;li&gt;View2.centerY == View1.centerY (constant: 0)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means that we can get the same layout using different sets of constraints.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 2
&lt;/h2&gt;

&lt;p&gt;We want a view that is inside a container view with a margin of 20 points.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w9YruCOc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/00df01cm6enfenovgjpy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w9YruCOc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/00df01cm6enfenovgjpy.png" alt="Tutos-Page-8"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;View2.top == View1.top (constant: 20)&lt;/li&gt;
&lt;li&gt;View2.trailing == View1.trailing (constant: 20)&lt;/li&gt;
&lt;li&gt;View2.bottom == View1.bottom (constant: 20)&lt;/li&gt;
&lt;li&gt;View2.leading == View1.leading (constant: 20)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example 3
&lt;/h2&gt;

&lt;p&gt;We want two views that are inside a third one, and both of them have the same size.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nGwCWn9M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6c188k6xtpskdum8aja5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nGwCWn9M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6c188k6xtpskdum8aja5.png" alt="Tutos-Page-9"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The solution for this problem is a bit more complex, so let's divide it in subproblems.&lt;/p&gt;

&lt;p&gt;The first thing we have to fix is how to get &lt;code&gt;View2&lt;/code&gt; to stay on the left part of the container, &lt;code&gt;View1&lt;/code&gt;. We can get that in a simple way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;View2.top == View1.top (constant: 0)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;View2.leading == View1.leading (constant: 0)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;View2.bottom == View1.bottom (constant: 0)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then, we can add do the same for &lt;code&gt;View3&lt;/code&gt;, in order to have that anchored to the right side of its container:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;View2.top == View1.top (constant: 0)&lt;/li&gt;
&lt;li&gt;View2.leading == View1.leading (constant: 0)&lt;/li&gt;
&lt;li&gt;View2.bottom == View1.bottom (constant: 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;View3.top == View1.top (constant: 0)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;View3.trailing == View1.trailing (constant: 0)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;View3.bottom == View1.bottom (constant: 0)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And finally, we need to disambiguate how those two views, View2 and View3 are related (they need to be equal in size).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;View2.top == View1.top (constant: 0)&lt;/li&gt;
&lt;li&gt;View2.leading == View1.leading (constant: 0)&lt;/li&gt;
&lt;li&gt;View2.bottom == View1.bottom (constant: 0)&lt;/li&gt;
&lt;li&gt;View3.top == View1.top (constant: 0)&lt;/li&gt;
&lt;li&gt;View3.trailing == View1.trailing (constant: 0)&lt;/li&gt;
&lt;li&gt;View3.bottom == View1.bottom (constant: 0)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;View2.width == View3.width (constant: 0)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;View2.trailing == View3.leading (constant: 0)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  How to create a project
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Open Xcode and click on &lt;strong&gt;File &amp;gt; New &amp;gt; Project...&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;iOS &amp;gt; App&lt;/strong&gt; in the newly opened window. Click on &lt;strong&gt;Next&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;In &lt;strong&gt;Product Name&lt;/strong&gt; insert the name of the application you'd like to create. In this case, "TutorialApp" is good enough. Don't pay attention to &lt;strong&gt;Team&lt;/strong&gt; and &lt;strong&gt;Organization Identifier&lt;/strong&gt; for now. &lt;strong&gt;Interface&lt;/strong&gt; should be &lt;strong&gt;Storyboard&lt;/strong&gt;, &lt;strong&gt;Language&lt;/strong&gt; should be &lt;strong&gt;Swift&lt;/strong&gt;, and let's unselect &lt;strong&gt;Use Core Data&lt;/strong&gt;  and &lt;strong&gt;Include Tests&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;Select any location in your disk you may want to save your project in, and click on &lt;strong&gt;Create&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;In the initial project you will find:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AppDelegate.swift&lt;/strong&gt;: It's a class where we'll have methods that are executed when something happened in our app (for instance, whenever the app goes to background). We can add custom code to those methods, in order to customize the app behavior.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SceneDelegate.swift&lt;/strong&gt;: Similar to AppDelegate (for now, let's assume this).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ViewController.swift&lt;/strong&gt;: An example &lt;strong&gt;UIViewController&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Main.storyboard&lt;/strong&gt;: We'll explain it in more detail in the next section. It's a file where the UI is described in a visual way.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Assets.xcassets&lt;/strong&gt;: It's where we'll save graphic resources for our app. Images, Colors, etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LaunchScreen.storyboard&lt;/strong&gt;: It's where we define the "Splash" screen of our app, the loading screen that's shown at the application startup. It will only count with a view, without a controller, and can't have any kind of interactivity or animations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Interface Builder
&lt;/h1&gt;

&lt;p&gt;UIs can be built programmatically. We can instantiate &lt;code&gt;UIView&lt;/code&gt; subclasses and add &lt;code&gt;NSLayoutConstraint&lt;/code&gt; (the constraints we've just learned) in plain Swift code. However, that can result in a lot of boilerplate. What's more, it isn't clear what kind of UI we're building in plain sight. &lt;br&gt;
Xcode has a bundled tool called &lt;strong&gt;Interface Builder&lt;/strong&gt;, which will allow us to develop user interfaces in a visual way. &lt;strong&gt;.storyboard&lt;/strong&gt; and &lt;strong&gt;.xib&lt;/strong&gt; files in our project are user interfaces descriptions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5mhpfWPN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imoy6qs2bris2cvnkppc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5mhpfWPN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imoy6qs2bris2cvnkppc.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once a storyboard or xib is opened in the Interface Builder, you can open the UIKit components library by pressing &lt;strong&gt;Cmd + Shift + L&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;Let's search for &lt;strong&gt;UIView&lt;/strong&gt; and select the first component that appears in the selector, and drag and drop it to our screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Bre-SjNt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0x0ulvudwkubqzzp1k74.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Bre-SjNt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0x0ulvudwkubqzzp1k74.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;In the right side of the screen, we have an editor for the view attributes, when we have the view selected.&lt;/p&gt;

&lt;p&gt;Let's change its background color.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5QG-WK1N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3rtovl0lgf9tyq2ru0vn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5QG-WK1N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3rtovl0lgf9tyq2ru0vn.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;In the lower part of the interface builder interface, we'll find some important controls to create constraints.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K0qh5Tok--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9g0kfp95ncsatqcuv66k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K0qh5Tok--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9g0kfp95ncsatqcuv66k.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the third option, we'll have tools to generate constraints for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;width/height (just constants, without comparing them to other views)&lt;/li&gt;
&lt;li&gt;top&lt;/li&gt;
&lt;li&gt;leading&lt;/li&gt;
&lt;li&gt;trailing&lt;/li&gt;
&lt;li&gt;bottom&lt;/li&gt;
&lt;li&gt;aspect ratio (height vs width inside the same view)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's always calculated from the view to the nearest neighbor.&lt;/p&gt;

&lt;p&gt;Let's keep &lt;strong&gt;Constraint to margins&lt;/strong&gt; always unchecked.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bY5xrDON--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9p6fflfeo4s7pj6t9m8i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bY5xrDON--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9p6fflfeo4s7pj6t9m8i.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Inside the second option, we'll find tools to define:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CenterX&lt;/li&gt;
&lt;li&gt;CenterY&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Let's create constraints to the top, leading, bottom, and trailing with 0 as their constant, so that view will have the same size as its container.&lt;/p&gt;

&lt;p&gt;After that, let's click on &lt;strong&gt;Add 4 constraints&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--baOxvVHx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32mt8z1ovy1meo056hp0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--baOxvVHx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32mt8z1ovy1meo056hp0.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we go to the sixth tab in the right panel, always maintaining the recently added view selected , we'll see its origin and size and the list of its constraints.&lt;/p&gt;

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

&lt;p&gt;If we do double-click on any of those constraints, we'll see its details, with all the components we've detailed previously.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: To delete that constraint, we can press &lt;strong&gt;delete&lt;/strong&gt; in our keyboards.&lt;/p&gt;

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

&lt;p&gt;The other option while generating constraints is to keep &lt;strong&gt;control&lt;/strong&gt; pressed, click on a view and drag and drop to another view.&lt;/p&gt;

&lt;p&gt;Some options will appear then, and we'll be able to edit them from the panel at the right.&lt;/p&gt;

&lt;p&gt;That will let us generate constraints of, for instance, width or height relative to another view.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xZYVAU9I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69j1mc3em1nlot5aszd2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xZYVAU9I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69j1mc3em1nlot5aszd2.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Practice
&lt;/h1&gt;

&lt;p&gt;In order to do the following exercises, we'll create views using simple colored rectangles, as we've seen in the section about &lt;strong&gt;Interface Builder&lt;/strong&gt; .&lt;/p&gt;

&lt;p&gt;You can practice with the examples I'll write in this section or with any example you can think of.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exercise 1
&lt;/h2&gt;

&lt;p&gt;A view with 20 pts of margin to its container&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oItUu-fR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5wpd4shjuorqmflcdz81.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oItUu-fR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5wpd4shjuorqmflcdz81.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Exercise 2
&lt;/h2&gt;

&lt;p&gt;Two views with the same height, one below the other:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UmRK5fn3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7oukd33lekbyrsl8ixh7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UmRK5fn3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7oukd33lekbyrsl8ixh7.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Exercise 3
&lt;/h1&gt;

&lt;p&gt;These weird examples &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B1hgdbLE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8cnrugezu83tctg5a1ag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B1hgdbLE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8cnrugezu83tctg5a1ag.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u-5642M2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ff55yp4sgm18h85pqqj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u-5642M2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ff55yp4sgm18h85pqqj.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or any Mondrian you like 😅&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GkHUIRqa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ugvytelmzm11e1qxtfn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GkHUIRqa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ugvytelmzm11e1qxtfn.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Notes and links on fixing font imports in iOS</title>
      <dc:creator>Fernando Martín Ortiz</dc:creator>
      <pubDate>Mon, 19 Apr 2021 22:02:19 +0000</pubDate>
      <link>https://dev.to/fmo91/notes-and-links-on-fixing-font-imports-in-ios-1jc7</link>
      <guid>https://dev.to/fmo91/notes-and-links-on-fixing-font-imports-in-ios-1jc7</guid>
      <description>&lt;p&gt;This will be a quick one. I won't explain how you should import a font in iOS. There are plenty of great resources in the internet. &lt;/p&gt;

&lt;p&gt;Here are two :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app"&gt;The Apple documentation&lt;/a&gt;: nicely explained. You can follow the guide and you won't have problems with it 99% of the times.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/"&gt;Common Mistakes With Adding Custom Fonts to Your iOS App&lt;/a&gt;: by Chris Chang, this has been my go-to guide when importing custom fonts since the beginning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So I won't talk about it. I won't repeat what brilliant people in the internet have already said.&lt;/p&gt;

&lt;p&gt;Today I had to import a custom font in a project. I imported the font as I have already done. Well, I imported 4 fonts to be clear. However, only two of them appeared when I run this code to log the fonts in the console:&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;for&lt;/span&gt; &lt;span class="n"&gt;familyName&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kt"&gt;UIFont&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;familyNames&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;"-------------"&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;"FAMILY: &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;familyName&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;fontName&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kt"&gt;UIFont&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fontNames&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;forFamilyName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;familyName&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="s"&gt;"- &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;fontName&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&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;p&gt;The other two were completely missing! I was doing something that haven't done in the past: I imported &lt;code&gt;.woff&lt;/code&gt; fonts. So the first things I thought was obvious, this format isn't completely supported in iOS. However, while the Apple documentation recommends using &lt;code&gt;.otf&lt;/code&gt; or &lt;code&gt;.ttf&lt;/code&gt; formats, &lt;code&gt;.woff&lt;/code&gt; is still supported since iOS 10.&lt;/p&gt;

&lt;p&gt;So, what was the problem? &lt;/p&gt;

&lt;p&gt;Let me tell you how I found it. I used https &lt;strong&gt;fontdrop&lt;/strong&gt;. This is an &lt;strong&gt;incredible&lt;/strong&gt; resource: &lt;a href="https://fontdrop.info/"&gt;https://fontdrop.info/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using fontdrop, you can just drag and drop your font, regardless of its format, and discover many metadata about the font, including its name, and guess what? &lt;strong&gt;The font I was trying to import had a completely random name&lt;/strong&gt;. Big facepalm for me.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lLzsrF8L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/45l2u9sryyglvojmp3tr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lLzsrF8L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/45l2u9sryyglvojmp3tr.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The fix for this could have included asking the designer to fix it for me. However, I tried to do it myself. &lt;br&gt;
Surprisingly, changing a font name is not very difficult. Using &lt;a href="https://fontforge.org/en-US/"&gt;FontForge&lt;/a&gt; for instance, you can download the open source tool, import your font, being patient with its Java-like UI, and you can select &lt;code&gt;Element &amp;gt; Font Info...&lt;/code&gt;, and change three parameters: &lt;code&gt;Fontname&lt;/code&gt;, &lt;code&gt;Family Name&lt;/code&gt; and &lt;code&gt;Name For Humans&lt;/code&gt; (which I suspect is the name you get when you are selecting the font from the interface builder).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JR-4ee21--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkectsgihznwtm2zx9t8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JR-4ee21--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkectsgihznwtm2zx9t8.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After editing those parameters, you can choose &lt;code&gt;File &amp;gt; Generate Fonts...&lt;/code&gt;, and export the font as True Type Font (i.e. ttf format).&lt;/p&gt;

&lt;p&gt;After that, I finally got to show the font in the console in my project!&lt;/p&gt;

&lt;p&gt;Now that you've read until here, let me show how to use the font in SwiftUI.&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;// In a new file...&lt;/span&gt;

&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;SwiftUI&lt;/span&gt;

&lt;span class="c1"&gt;// The SwiftUI font type&lt;/span&gt;
&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;Font&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;customSemibold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ofSize&lt;/span&gt; &lt;span class="nv"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;CGFloat&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Font&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;custom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SomeCustomFont-Semibold"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;size&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;p&gt;And you can add as many static functions as needed depending on the number of custom font types.&lt;/p&gt;

&lt;p&gt;Inside your views, you can use this font using something as simple as this:&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;ACustomView&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;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Some Text"&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="nf"&gt;customSemibold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;ofSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;14.0&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;p&gt;I sincerely hope this can help anyone at some point. &lt;/p&gt;

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