<?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: Lou Franco</title>
    <description>The latest articles on DEV Community by Lou Franco (@loufranco).</description>
    <link>https://dev.to/loufranco</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%2F229669%2F39fb9cff-7b13-402b-8e9d-6c3a9b49aef1.jpeg</url>
      <title>DEV Community: Lou Franco</title>
      <link>https://dev.to/loufranco</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/loufranco"/>
    <language>en</language>
    <item>
      <title>The Swift Programming Language Companion: Methods</title>
      <dc:creator>Lou Franco</dc:creator>
      <pubDate>Wed, 13 Jan 2021 11:40:43 +0000</pubDate>
      <link>https://dev.to/loufranco/the-swift-programming-language-companion-methods-36pd</link>
      <guid>https://dev.to/loufranco/the-swift-programming-language-companion-methods-36pd</guid>
      <description>&lt;p&gt;This article is part of a series on learning Swift by writing code to The Swift Programming Language book from Apple. This is the sixth article in Section 2 (here is &lt;a href="https://app-o-mat.com/post/swift-programming-language-companion-section-1"&gt;Section 1&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Read each article after you have read the corresponding chapter in the book. This article is a companion to &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Methods.html"&gt;Methods&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up a reading environment
&lt;/h3&gt;

&lt;p&gt;If you are jumping around these articles, make sure you read the &lt;a href="https://dev.to/loufranco/the-swift-programming-language-companion-introduction-4k0a"&gt;Introduction&lt;/a&gt; to see my recommendation for setting up a reading environment.&lt;/p&gt;

&lt;p&gt;To add a new page, in Xcode:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose File &amp;gt; New &amp;gt; Playground Page&lt;/li&gt;
&lt;li&gt;Rename the page to "11-Methods"&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Exercises for Methods
&lt;/h3&gt;

&lt;p&gt;At this point, you should have read &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Methods.html"&gt;Methods&lt;/a&gt; in The Swift Programming Language. You should have a Playground page for this chapter with code in it that you generated while reading the book.&lt;/p&gt;

&lt;h4&gt;
  
  
  Exercises
&lt;/h4&gt;

&lt;p&gt;The chapter covers how to declare Methods inside of Structures and Classes&lt;/p&gt;

&lt;p&gt;For these exercises, we are going to imagine a simple media playing app. From the last two chapters, you should have 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;Song&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;title&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="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Player&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;currentSong&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Song&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;didSet&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;currentSong&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;currentSong&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;currentSong&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; is playing"&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;"No song is playing"&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your Playground write code to do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add a method to &lt;code&gt;Player&lt;/code&gt; called &lt;code&gt;isPlaying&lt;/code&gt; that returns a &lt;code&gt;Bool&lt;/code&gt;. It should be true if &lt;code&gt;currentSong&lt;/code&gt; is not nil.&lt;/li&gt;
&lt;li&gt;Add a method to &lt;code&gt;Player&lt;/code&gt; called &lt;code&gt;play&lt;/code&gt; that takes a &lt;code&gt;song&lt;/code&gt; parameter. It should set &lt;code&gt;currentSong&lt;/code&gt;. The parameter should not be optional.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Harder challenge:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add methods &lt;code&gt;pause&lt;/code&gt; and &lt;code&gt;resume&lt;/code&gt; (taking no parameters and returning &lt;code&gt;Void&lt;/code&gt;). To do this, you might need more properties. Make sure &lt;code&gt;isPlaying&lt;/code&gt; and &lt;code&gt;currentSong&lt;/code&gt; behave reasonably.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Next
&lt;/h4&gt;

&lt;p&gt;The next article will provide exercises for the &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Subscripts.html"&gt;Subscripts&lt;/a&gt; chapter.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>beginners</category>
      <category>swift</category>
    </item>
    <item>
      <title>The Swift Programming Language Companion: Properties Part 2</title>
      <dc:creator>Lou Franco</dc:creator>
      <pubDate>Sat, 27 Jun 2020 12:42:05 +0000</pubDate>
      <link>https://dev.to/loufranco/the-swift-programming-language-companion-properties-part-2-5dbn</link>
      <guid>https://dev.to/loufranco/the-swift-programming-language-companion-properties-part-2-5dbn</guid>
      <description>&lt;p&gt;This article is part of a series on learning Swift by writing code to The Swift Programming Language book from Apple. This is the fifth article in Section 2 (here is &lt;a href="https://app-o-mat.com/post/swift-programming-language-companion-section-1"&gt;Section 1&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Read each article after you have read the corresponding chapter in the book. This article is a companion to &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Properties.html"&gt;Properties&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is Part 2 of the Properties companion. It builds on &lt;a href="https://app-o-mat.com/post/swift-book-companion-properties-1"&gt;Properties Part 1&lt;/a&gt;, so read that if you have not and do the exercises. The exercises in this chapter require that you have done those already.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up a reading environment
&lt;/h3&gt;

&lt;p&gt;If you are jumping around these articles, make sure you read the &lt;a href="https://dev.to/loufranco/the-swift-programming-language-companion-introduction-4k0a"&gt;Introduction&lt;/a&gt; to see my recommendation for setting up a reading environment.&lt;/p&gt;

&lt;p&gt;We can continuing using the page we made for Part 1 ("10-Properties").&lt;/p&gt;

&lt;h3&gt;
  
  
  Exercises for Properties
&lt;/h3&gt;

&lt;p&gt;At this point, you should have read &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Properties.html"&gt;Properties&lt;/a&gt; in The Swift Programming Language. You should have a Playground page for this chapter with code in it that you generated while reading the book.&lt;/p&gt;

&lt;h4&gt;
  
  
  Exercises
&lt;/h4&gt;

&lt;p&gt;The chapter covers how to declare Properties inside of Structures and Classes. In Part 1, we had exercised for stored properties, computed properties, and Type properties.&lt;/p&gt;

&lt;p&gt;In Part 2, we’re going to look at Property Observers.&lt;/p&gt;

&lt;p&gt;For these exercises, we are going to continue with the code we built in the last chapter.&lt;/p&gt;

&lt;p&gt;In your Playground write code to do the following in the same playground that you used for Properties Part 1:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Declare a &lt;code&gt;Player&lt;/code&gt; class&lt;/li&gt;
&lt;li&gt;Add a &lt;code&gt;var&lt;/code&gt; property to &lt;code&gt;Player&lt;/code&gt; called &lt;code&gt;currentSong&lt;/code&gt; that is of type &lt;code&gt;Song?&lt;/code&gt; (we created &lt;code&gt;Song&lt;/code&gt; in the last chapter)&lt;/li&gt;
&lt;li&gt;Add a &lt;code&gt;didSet&lt;/code&gt; property observer to &lt;code&gt;currentSong&lt;/code&gt;. In it, print the title of the &lt;code&gt;currentSong&lt;/code&gt; with “ is playing” appended to it (use String interpolation)&lt;/li&gt;
&lt;li&gt;Construct a &lt;code&gt;Player&lt;/code&gt; and set &lt;code&gt;currentSong&lt;/code&gt; to a few different &lt;code&gt;Song&lt;/code&gt; values.&lt;/li&gt;
&lt;li&gt;In your &lt;code&gt;didSet&lt;/code&gt;, when the new value is &lt;code&gt;nil&lt;/code&gt;, print “No song is playing”&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;currentSong&lt;/code&gt; of your &lt;code&gt;Player&lt;/code&gt; object to nil&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Next
&lt;/h4&gt;

&lt;p&gt;The next article will provide exercises for the &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Methods.html"&gt;Methods&lt;/a&gt; chapter.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Swift Programming Language Companion: Properties Part 1</title>
      <dc:creator>Lou Franco</dc:creator>
      <pubDate>Tue, 09 Jun 2020 12:58:35 +0000</pubDate>
      <link>https://dev.to/loufranco/the-swift-programming-language-companion-properties-part-1-b94</link>
      <guid>https://dev.to/loufranco/the-swift-programming-language-companion-properties-part-1-b94</guid>
      <description>&lt;p&gt;This article is part of a series on learning Swift by writing code to The Swift Programming Language book from Apple. This is the fourth article in Section 2 (here is &lt;a href="https://app-o-mat.com/post/swift-programming-language-companion-section-1"&gt;Section 1&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Read each article after you have read the corresponding chapter in the book. This article is a companion to &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Properties.html"&gt;Properties&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up a reading environment
&lt;/h3&gt;

&lt;p&gt;If you are jumping around these articles, make sure you read the &lt;a href="https://dev.to/loufranco/the-swift-programming-language-companion-introduction-4k0a"&gt;Introduction&lt;/a&gt; to see my recommendation for setting up a reading environment.&lt;/p&gt;

&lt;p&gt;To add a new page, in Xcode:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose File &amp;gt; New &amp;gt; Playground Page&lt;/li&gt;
&lt;li&gt;Rename the page to "10-Properties"&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Exercises for Properties
&lt;/h3&gt;

&lt;p&gt;At this point, you should have read &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Properties.html"&gt;Properties&lt;/a&gt; in The Swift Programming Language. You should have a Playground page for this chapter with code in it that you generated while reading the book.&lt;/p&gt;

&lt;h4&gt;
  
  
  Exercises
&lt;/h4&gt;

&lt;p&gt;The chapter covers how to declare Properties inside of Structures and Classes. For Part 1, we're going to stick to the more common parts, stored properties, computed properties, and Type properties.&lt;/p&gt;

&lt;p&gt;For these exercises, we are going to imagine a simple media playing app.&lt;/p&gt;

&lt;p&gt;In your Playground write code to do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;(from enumerations): Declare an enumeration called &lt;code&gt;Genre&lt;/code&gt; and add cases for &lt;code&gt;jazz&lt;/code&gt;, &lt;code&gt;rock&lt;/code&gt;, &lt;code&gt;rap&lt;/code&gt;, &lt;code&gt;country&lt;/code&gt;, and &lt;code&gt;folk&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Declare an &lt;code&gt;Artist&lt;/code&gt; struct with stored properties &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;birthDate&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Declare a &lt;code&gt;Song&lt;/code&gt; struct with stored properties &lt;code&gt;title&lt;/code&gt;, &lt;code&gt;artist&lt;/code&gt;, and &lt;code&gt;genre&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add a computed property to &lt;code&gt;Artist&lt;/code&gt; called &lt;code&gt;age&lt;/code&gt; which uses &lt;code&gt;birthDate&lt;/code&gt; and today’s date.&lt;/li&gt;
&lt;li&gt;Declare a few &lt;code&gt;Artist&lt;/code&gt; and &lt;code&gt;Song&lt;/code&gt; instances&lt;/li&gt;
&lt;li&gt;Make a class called &lt;code&gt;Library&lt;/code&gt; and add a Type property called &lt;code&gt;songs&lt;/code&gt; that is an array of &lt;code&gt;Song&lt;/code&gt; structs. Initialize it with a few values.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Hints
&lt;/h4&gt;

&lt;p&gt;To find today’s date: &lt;code&gt;let today = Date()&lt;/code&gt;&lt;br&gt;
To find the number of seconds between two dates:&lt;br&gt;
&lt;code&gt;let seconds = today.timeIntervalSince(birthDate)&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Next
&lt;/h4&gt;

&lt;p&gt;The next article will look at the more advanced usage of properties.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Swift Programming Language Companion: Structures and Classes</title>
      <dc:creator>Lou Franco</dc:creator>
      <pubDate>Mon, 08 Jun 2020 14:01:30 +0000</pubDate>
      <link>https://dev.to/loufranco/the-swift-programming-language-companion-structures-and-classes-48gj</link>
      <guid>https://dev.to/loufranco/the-swift-programming-language-companion-structures-and-classes-48gj</guid>
      <description>&lt;p&gt;This article is part of a series on learning Swift by writing code to The Swift Programming Language book from Apple. This is the third article in Section 2 (here is &lt;a href="https://app-o-mat.com/post/swift-programming-language-companion-section-1"&gt;Section 1&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Read each article after you have read the corresponding chapter in the book. This article is a companion to &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/ClassesAndStructures.html"&gt;Structures and Classes&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up a reading environment
&lt;/h3&gt;

&lt;p&gt;If you are jumping around these articles, make sure you read the &lt;a href="https://dev.to/loufranco/the-swift-programming-language-companion-introduction-4k0a"&gt;Introduction&lt;/a&gt; to see my recommendation for setting up a reading environment.&lt;/p&gt;

&lt;p&gt;To add a new page, in Xcode:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose File &amp;gt; New &amp;gt; Playground Page&lt;/li&gt;
&lt;li&gt;Rename the page to "09-Structs-Classes"&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Exercises for Structures and Classes
&lt;/h3&gt;

&lt;p&gt;At this point, you should have read &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/ClassesAndStructures.html"&gt;Structures and Classes&lt;/a&gt; in The Swift Programming Language. You should have a Playground page for this chapter with code in it that you generated while reading the book.&lt;/p&gt;

&lt;h4&gt;
  
  
  Review
&lt;/h4&gt;

&lt;p&gt;This chapter is not so much about how to make Structures and Classes, but more about the difference between them. They both have properties and methods (as we'll see in the next two chapters), and are sometimes interchangeable. To beginners, it can be confusing to tell the difference between them or decide when to use one over the other.&lt;/p&gt;

&lt;p&gt;Structure instances are values. We have already seen many other values, like &lt;code&gt;Int&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, and enumerations. Values can never change. &lt;code&gt;5&lt;/code&gt; is a value, and it makes no sense to change &lt;code&gt;5&lt;/code&gt; to &lt;code&gt;6&lt;/code&gt; -- I can add the value &lt;code&gt;1&lt;/code&gt; to the value &lt;code&gt;5&lt;/code&gt; and get a new value &lt;code&gt;6&lt;/code&gt;, but I can't change &lt;code&gt;5&lt;/code&gt;. I can't change &lt;code&gt;"Hello"&lt;/code&gt; and I can't change &lt;code&gt;June 8, 2020&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If I have a variable that is set to a value, I can change which value it is set to, but I cannot change the value itself. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = 5
var b = a
a += 1 // b is still 5 -- a refers to a new value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Class instances are objects with identity. It is possible to change them, and individual objects can be told apart (whereas every equal value is completely indistinguishable). If multiple variables refer to the same object, and we change it, we can see that change using any variable that refers to that same object.&lt;/p&gt;

&lt;p&gt;Even if two objects are equivalent, they might not be identical. If I copy an object to a new one, the copy will not change if I change the original one.&lt;/p&gt;

&lt;p&gt;You will learn when to use one or the other through experience. To start with, try to favor structs over classes.&lt;/p&gt;

&lt;h4&gt;
  
  
  Exercises
&lt;/h4&gt;

&lt;p&gt;The chapter covers how to declare Structures and Classes and the differences between them.&lt;/p&gt;

&lt;p&gt;For these exercises, we are going to imagine a simple media playing app.&lt;/p&gt;

&lt;p&gt;In your Playground write code to do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make a struct that represents a song (name it &lt;code&gt;SongStruct&lt;/code&gt;). Songs have a &lt;code&gt;title&lt;/code&gt;, an &lt;code&gt;artist&lt;/code&gt;, and a &lt;code&gt;releaseDate&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Make an array of &lt;code&gt;SongStruct&lt;/code&gt; values.&lt;/li&gt;
&lt;li&gt;Make a class that represents a song (name it &lt;code&gt;SongClass&lt;/code&gt;). Add the same three properties&lt;/li&gt;
&lt;li&gt;Make an array of &lt;code&gt;SongClass&lt;/code&gt; objects.&lt;/li&gt;
&lt;li&gt;Write code to explore the difference between them. 

&lt;ol&gt;
&lt;li&gt;Change the properties in the two types from &lt;code&gt;let&lt;/code&gt; to &lt;code&gt;var&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Make variables that are set to instances of each and change the properties&lt;/li&gt;
&lt;li&gt;Do the same thing, but set other variables to the ones you make (&lt;code&gt;var b = a&lt;/code&gt;) first and see if changing &lt;code&gt;a&lt;/code&gt; changes &lt;code&gt;b&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Try to change the instances inside the arrays&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Next
&lt;/h4&gt;

&lt;p&gt;The next article will provide exercises for the &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Properties.html"&gt;Properties&lt;/a&gt; chapter.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Swift Programming Language Companion: Enumerations</title>
      <dc:creator>Lou Franco</dc:creator>
      <pubDate>Sun, 07 Jun 2020 13:57:09 +0000</pubDate>
      <link>https://dev.to/loufranco/the-swift-programming-language-companion-enumerations-2p2m</link>
      <guid>https://dev.to/loufranco/the-swift-programming-language-companion-enumerations-2p2m</guid>
      <description>&lt;p&gt;This article is part of a series on learning Swift by writing code to The Swift Programming Language book from Apple. This is the second article in Section 2 (here is &lt;a href="https://app-o-mat.com/post/swift-programming-language-companion-section-1"&gt;Section 1&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Read each article after you have read the corresponding chapter in the book. This article is a companion to &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html"&gt;Enumerations&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up a reading environment
&lt;/h3&gt;

&lt;p&gt;If you are jumping around these articles, make sure you read the &lt;a href="https://dev.to/loufranco/the-swift-programming-language-companion-introduction-4k0a"&gt;Introduction&lt;/a&gt; to see my recommendation for setting up a reading environment.&lt;/p&gt;

&lt;p&gt;To add a new page, in Xcode:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose File &amp;gt; New &amp;gt; Playground Page&lt;/li&gt;
&lt;li&gt;Rename the page to "08-Enumerations"&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Exercises for Enumerations
&lt;/h3&gt;

&lt;p&gt;At this point, you should have read &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html"&gt;Enumerations&lt;/a&gt; in The Swift Programming Language. You should have a Playground page for this chapter with code in it that you generated while reading the book.&lt;/p&gt;

&lt;h4&gt;
  
  
  Exercises
&lt;/h4&gt;

&lt;p&gt;The chapter covers how to declare Enumeration types and use Associated Values.&lt;/p&gt;

&lt;p&gt;For these exercises, we are going to imagine a simple media playing app.&lt;/p&gt;

&lt;p&gt;In your Playground write code to do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Declare an enumeration called &lt;code&gt;Genre&lt;/code&gt; and add cases for &lt;code&gt;jazz&lt;/code&gt;, &lt;code&gt;rock&lt;/code&gt;, &lt;code&gt;rap&lt;/code&gt;, &lt;code&gt;country&lt;/code&gt;, and &lt;code&gt;folk&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Write a function called &lt;code&gt;genreName&lt;/code&gt; that takes a &lt;code&gt;Genre&lt;/code&gt; value and uses a &lt;code&gt;switch&lt;/code&gt; to return a string with the name of the genre. So, "Jazz" for &lt;code&gt;.jazz&lt;/code&gt; and "Rap" for &lt;code&gt;.rap&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;genreName&lt;/code&gt; with some &lt;code&gt;Genre&lt;/code&gt; values.&lt;/li&gt;
&lt;li&gt;Extend &lt;code&gt;Genre&lt;/code&gt; to implement &lt;code&gt;CaseIterable&lt;/code&gt; and write a &lt;code&gt;for in&lt;/code&gt; loop to print out all of the genres using &lt;code&gt;Genre.allCases&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Change &lt;code&gt;Genre&lt;/code&gt; to extend &lt;code&gt;String&lt;/code&gt; and change &lt;code&gt;genreName&lt;/code&gt; to use &lt;code&gt;genre.rawValue&lt;/code&gt; instead of a &lt;code&gt;switch&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Make another enumeration called &lt;code&gt;MediaType&lt;/code&gt; and add cases for &lt;code&gt;music&lt;/code&gt;, &lt;code&gt;podcast&lt;/code&gt;, &lt;code&gt;audioBook&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add an associated value for &lt;code&gt;music&lt;/code&gt; of type &lt;code&gt;Genre&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Write a function called &lt;code&gt;mediaTitle&lt;/code&gt; that takes a &lt;code&gt;MediaType&lt;/code&gt; value and uses a &lt;code&gt;switch&lt;/code&gt; to return a string with the name of the name of the media type. If it's &lt;code&gt;music&lt;/code&gt;, make sure to use the associated &lt;code&gt;genre&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;mediaTitle&lt;/code&gt; with some &lt;code&gt;MediaType&lt;/code&gt; values.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Next
&lt;/h4&gt;

&lt;p&gt;The next article will provide exercises for the &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/ClassesAndStructures.html"&gt;Structures and Classes&lt;/a&gt; chapter.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Swift Programming Language Companion: Closures</title>
      <dc:creator>Lou Franco</dc:creator>
      <pubDate>Sat, 06 Jun 2020 19:28:28 +0000</pubDate>
      <link>https://dev.to/loufranco/the-swift-programming-language-companion-closures-4p4m</link>
      <guid>https://dev.to/loufranco/the-swift-programming-language-companion-closures-4p4m</guid>
      <description>&lt;p&gt;This article is part of a series on learning Swift by writing code to The Swift Programming Language book from Apple. This is the first article in Section 2 (here is &lt;a href="https://app-o-mat.com/post/swift-programming-language-companion-section-1"&gt;Section 1&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Read each article after you have read the corresponding chapter in the book. This article is a companion to &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Closures.html"&gt;Closures&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up a reading environment
&lt;/h3&gt;

&lt;p&gt;If you are jumping around these articles, make sure you read the &lt;a href="https://dev.to/loufranco/the-swift-programming-language-companion-introduction-4k0a"&gt;Introduction&lt;/a&gt; to see my recommendation for setting up a reading environment.&lt;/p&gt;

&lt;p&gt;To add a new page, in Xcode:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose File &amp;gt; New &amp;gt; Playground Page&lt;/li&gt;
&lt;li&gt;Rename the page to "07-Closures"&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Exercises for Closures
&lt;/h3&gt;

&lt;p&gt;At this point, you should have read &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Closures.html"&gt;Closures&lt;/a&gt; in The Swift Programming Language. You should have a Playground page for this chapter with code in it that you generated while reading the book.&lt;/p&gt;

&lt;h4&gt;
  
  
  Exercises
&lt;/h4&gt;

&lt;p&gt;The chapter covers how to create and call closures and the concept of &lt;em&gt;capturing&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For these exercises, we are going to imagine a simple media playing app.&lt;/p&gt;

&lt;p&gt;In your Playground write code to do the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Declare a Boolean variable called &lt;code&gt;isPlaying&lt;/code&gt; and set it to &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Declare a constant called &lt;code&gt;play&lt;/code&gt; that is a closure type that takes no arguments and returns &lt;code&gt;Void&lt;/code&gt;. Initialize it with a closure that prints the word "Play" and sets &lt;code&gt;isPlaying&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt; when called.&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;play&lt;/code&gt;. Print &lt;code&gt;isPlaying&lt;/code&gt; (it should be true)&lt;/li&gt;
&lt;li&gt;Make a constant called &lt;code&gt;stop&lt;/code&gt; which is like &lt;code&gt;play&lt;/code&gt; but prints "Stop" and sets &lt;code&gt;isPlaying&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Call &lt;code&gt;stop&lt;/code&gt; and print &lt;code&gt;isPlaying&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Under &lt;code&gt;isPlaying&lt;/code&gt;, Declare an optional &lt;code&gt;String&lt;/code&gt; variable called &lt;code&gt;currentSong&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add a &lt;code&gt;String&lt;/code&gt; argument to play called &lt;code&gt;song&lt;/code&gt; and set &lt;code&gt;currentSong&lt;/code&gt; to it inside the closure.&lt;/li&gt;
&lt;li&gt;Update your call to play pass a &lt;code&gt;song&lt;/code&gt; argument&lt;/li&gt;
&lt;li&gt;Print &lt;code&gt;currentSong&lt;/code&gt; after calling &lt;code&gt;play&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Declare a &lt;code&gt;playList&lt;/code&gt; array of &lt;code&gt;Strings&lt;/code&gt; and initialize it with 5 songs.&lt;/li&gt;
&lt;li&gt;Sort the list by the length of the song name&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Next
&lt;/h4&gt;

&lt;p&gt;The next article will provide exercises for the &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html"&gt;Enumerations&lt;/a&gt; chapter.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>beginners</category>
    </item>
    <item>
      <title>SpriteKit Gotcha: Taps Near the Bottom of the Screen</title>
      <dc:creator>Lou Franco</dc:creator>
      <pubDate>Sun, 09 Feb 2020 23:48:08 +0000</pubDate>
      <link>https://dev.to/loufranco/spritekit-gotcha-taps-near-the-bottom-of-the-screen-4len</link>
      <guid>https://dev.to/loufranco/spritekit-gotcha-taps-near-the-bottom-of-the-screen-4len</guid>
      <description>&lt;p&gt;Last month I released a simple, free educational game, &lt;a href="https://app-o-mat.com/app/math-o-mat"&gt;Math-o-Mat&lt;/a&gt;, to the App Store. It's basically like Pong, except you have to do arithmetic to "hit" the ball.&lt;/p&gt;

&lt;p&gt;This week, two kids got in touch with me through their parents (friends of mine) to tell me that &lt;em&gt;sometimes&lt;/em&gt; button taps weren't working for them.&lt;/p&gt;

&lt;p&gt;I play Math-o-Mat a lot, so I was a little surprised. I asked a couple of questions (what device, iOS version, etc), and I asked their parents if they generally have problems tapping buttons in other apps (they didn't).&lt;/p&gt;

&lt;p&gt;One thing that was in common is that the devices were pre-iPhone X, so they had Home buttons. I play Math-o-Mat on devices like that, but mostly in the simulator.&lt;/p&gt;

&lt;p&gt;I tried it on a test device, and rarely (but sometimes), I did seem to have a button tapping issue. It had something to do with this device, but also something to do with the exact way I was tapping.&lt;/p&gt;

&lt;p&gt;I logged all button taps, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public override func touchesBegan(_ touches: Set&amp;lt;UITouch&amp;gt;, with event: UIEvent?) {
    print("touchesBegan")
    self.onTap?(self)
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And noticed something weird ..... For buttons close to the bottom of the screen, the log would happen after a little delay if I didn't release the tap quickly.&lt;/p&gt;

&lt;p&gt;For other buttons (or on an iPhone X), &lt;code&gt;touchesBegan&lt;/code&gt; got called immediately when the touch started.&lt;/p&gt;

&lt;p&gt;Armed with some knowledge, I could now google and found the question &lt;a href="https://stackoverflow.com/q/19799961/3937"&gt;"UISystemGateGestureRecognizer and delayed taps near bottom of screen"&lt;/a&gt; on StackOverflow.&lt;/p&gt;

&lt;p&gt;So, iOS installs some gesture recognizers at the top level that try to help stop accidental gestures at the bottom edge on these phones. The suggested answer is to muck with the iOS &lt;code&gt;Window.gestureRecognizers&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Generally, I try never to do things like this because they are unsupported and will probably break in a future version of iOS. In fact, if you do it, you'll get a console message that says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[Warning] Trying to set delaysTouchesBegan to NO on a system gate gesture recognizer - this is unsupported and will have undesired side effects&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Luckily, there's another way to do it. Using &lt;code&gt;touchesBegan&lt;/code&gt; directly has this issue, but gesture recognizers do not. By default, SpriteKit tends not to use them, but it is completely possible.&lt;/p&gt;

&lt;p&gt;If you try to use &lt;code&gt;UIGestureRecognizer&lt;/code&gt; with SpriteKit, here are a couple of things to think about:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;SKNode&lt;/code&gt; is not a view, so you won't be able to attach them to your game nodes. My game is simple, so I attached them the main top level view and then used the &lt;code&gt;SKNode&lt;/code&gt; tree to find the right node (using &lt;code&gt;SKNode.nodes(at: CGPoint)&lt;/code&gt; on the root node).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The y-axis is flipped for the recognizer's touch point and the coordinate system used by SceneKit.  You need to use &lt;code&gt;view.height - tapPoint.y&lt;/code&gt; for your y-coordinate in the above call.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This also explains why I have never seen this behavior before (the delayed taps at the bottom of the screen). I generally just use &lt;code&gt;UIView&lt;/code&gt; or &lt;code&gt;UIGestureRecognizer&lt;/code&gt; descendants to get taps. It's been a very long time since I needed to implement something on top of &lt;code&gt;touchesBegan&lt;/code&gt; directly. SpriteKit uses neither of these classes, so the default way to collect taps is the low-level calls.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>beginners</category>
      <category>swift</category>
    </item>
    <item>
      <title>iOS StackOverflow Deep Dive: Retain Cycle in Closure</title>
      <dc:creator>Lou Franco</dc:creator>
      <pubDate>Sun, 02 Feb 2020 21:03:54 +0000</pubDate>
      <link>https://dev.to/loufranco/stackoverflow-deep-dive-retain-cycle-in-closure-ml6</link>
      <guid>https://dev.to/loufranco/stackoverflow-deep-dive-retain-cycle-in-closure-ml6</guid>
      <description>&lt;p&gt;Take a look at this &lt;a href="https://stackoverflow.com/q/59531298/3937"&gt;StackOverflow question&lt;/a&gt; (don't read the answers or comments) -- see if you can see the problem.&lt;/p&gt;

&lt;p&gt;Here are the clues I saw:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The title of the question says there is a retain cycle in a closure. It's not always good to trust the questioner, but ....&lt;/li&gt;
&lt;li&gt;They show a screen capture of the memory debugger that does show that that a closure is holding a strong reference to something holding it (causing the cycle)&lt;/li&gt;
&lt;li&gt;In all of the code shown, there is only one closure. Again, it's not always right to trust the questioner, but still a place to start.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's the closure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   coord.deinitIfNeeded = { [weak self] in
        guard let self = self else { return }
        self.free(coord)
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And here is the definition of Coordinator (the class of coord)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Coordinator {
    var deinitIfNeeded: (() -&amp;gt; ())?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Ok -- this is enough to solve the problem, if you want to try.&lt;/p&gt;

&lt;p&gt;Before I get to the answer: I want to give some advice to new programmers: Unless you know better, trust the tools (like the compiler and memory debugger). I'm not saying they are always right, but I am saying they are much more likely than you to be right. While you are learning, start by assuming your code is wrong.&lt;/p&gt;

&lt;p&gt;Ok, so the closure is holding a reference to something that is holding a reference to it.  The only thing holding reference to it is &lt;code&gt;coord&lt;/code&gt;, in the code: &lt;code&gt;coord.deinitIfNeeded = ...&lt;/code&gt;. So, is the closure holding &lt;code&gt;coord&lt;/code&gt; strongly?&lt;/p&gt;

&lt;p&gt;Yes, it is.&lt;/p&gt;

&lt;p&gt;It captured &lt;code&gt;coord&lt;/code&gt; in the body and &lt;code&gt;coord&lt;/code&gt; is not specified to be a weak capture.  Think about how to fix this and then check your work by &lt;a href="https://stackoverflow.com/a/59531728/3937"&gt;looking at the answer&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Swift Programming Language Companion: Review of Basics to Functions (Part 2)</title>
      <dc:creator>Lou Franco</dc:creator>
      <pubDate>Sun, 02 Feb 2020 17:37:54 +0000</pubDate>
      <link>https://dev.to/loufranco/the-swift-programming-language-companion-review-of-basics-to-functions-part-2-31k8</link>
      <guid>https://dev.to/loufranco/the-swift-programming-language-companion-review-of-basics-to-functions-part-2-31k8</guid>
      <description>&lt;p&gt;This article is part of a series on learning Swift by writing code to &lt;em&gt;The Swift Programming Language&lt;/em&gt; book from Apple.&lt;/p&gt;

&lt;p&gt;We've covered the basics up to functions, and reviewed them in the &lt;a href="https://dev.to/loufranco/the-swift-programming-language-companion-review-of-basics-to-functions-359"&gt;last article&lt;/a&gt;, and we're going to continue reviewing those chapters in this one.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up a reading environment
&lt;/h3&gt;

&lt;p&gt;If you are jumping around these articles, make sure you read the &lt;a href="https://dev.to/loufranco/the-swift-programming-language-companion-introduction-4k0a"&gt;Introduction&lt;/a&gt; to see my recommendation for setting up a reading environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exercises
&lt;/h3&gt;

&lt;p&gt;For this chapter, we're going to write code inspired by the Maps app.&lt;/p&gt;

&lt;p&gt;Copy this into your playground&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let places: [[String: Any]] = [
    ["name": "Buckingham Palace", "location": (lat: 51.5000632, long: -0.1449567)],
    ["name": "Great Pyramid of Giza", "location": (lat: 29.9792391, long: 31.1320079)],
    ["name": "Sydney Opera House", "location": (lat: -33.8567799, long: 151.2131027)],
    ["name": "Machu Picchu", "location": (lat: -13.163136, long: -72.5471569)],
    ["name": "Victoria Falls", "location": (lat: -17.9149133, long: 25.8379429)],
    ["name": "Empire State Building", "location": (lat: 40.7484445, long: -73.9878584)],
    ["name": "The Forbidden City", "location": (lat: 39.9394501, long: 116.1152174)],
    ["name": "Parthenon", "location": (lat: 38.3336346, long: 20.9712001)],
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make sure you understand how to access data in this structure, start with this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name = places[0]["name"] as? String
let location = places[0]["location"] as? (lat: Double, long: Double)
location?.lat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your Playground write code to do the following&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Print out the longitude (&lt;code&gt;.long&lt;/code&gt;) of The Sydney Opera House.&lt;/li&gt;
&lt;li&gt;Use &lt;a href="https://www.movable-type.co.uk/scripts/latlong.html"&gt;this page to find the distance between two of these places&lt;/a&gt; by plugging in their latitudes and longitudes.&lt;/li&gt;
&lt;li&gt;Implement the Haversine distance formula. If you are an experienced programmer (just new to Swift), then read the Javascript version on that page and try to port it.&lt;/li&gt;
&lt;li&gt;Once you have that, write a function that takes the &lt;code&gt;places&lt;/code&gt; data and returns the two places that are closest to each other. (In Swift, the basic math functions are all global functions: &lt;code&gt;sin&lt;/code&gt;, &lt;code&gt;cos&lt;/code&gt;, &lt;code&gt;atan2&lt;/code&gt;, &lt;code&gt;pow&lt;/code&gt;, etc)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;More help for beginners&lt;/p&gt;

&lt;p&gt;Ok, step one is to declare a function that takes two lat/long tuples and returns a &lt;code&gt;Double&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Start with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let location1 = places[0]["location"] as? (lat: Double, long:Double)
let location2 = places[1]["location"] as? (lat: Double, long:Double)

func distanceKm(loc1: (lat: Double, long: Double)?, loc2: (lat: Double, long: Double)?) 
-&amp;gt; Double? {
    if let loc1 = loc1, let loc2 = loc2 {
        // Implement Haversine here
        return 0 // .. replace this line
    }
    return 0
}

distanceKm(loc1: location1, loc2: location2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the basic algorithm:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Declare a constant &lt;code&gt;R&lt;/code&gt; which is 6371000.0 (the radius of the Earth)&lt;/li&gt;
&lt;li&gt;Declare a constant &lt;code&gt;phi1&lt;/code&gt; which is &lt;code&gt;loc1.lat&lt;/code&gt; in radians: &lt;code&gt;loc1.lat * .pi / 180.0&lt;/code&gt; (in general: &lt;code&gt;radians = degrees * .pi / 180.0&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Declare a constant &lt;code&gt;phi2&lt;/code&gt; which is &lt;code&gt;loc2.lat&lt;/code&gt; in radians&lt;/li&gt;
&lt;li&gt;Declare a constant &lt;code&gt;deltaPhi&lt;/code&gt; which is &lt;code&gt;(loc2.lat - loc1.lat)&lt;/code&gt; in radians&lt;/li&gt;
&lt;li&gt;Declare a constant &lt;code&gt;deltaLambda&lt;/code&gt; which is &lt;code&gt;(loc2.long - loc1.long)&lt;/code&gt; in radians&lt;/li&gt;
&lt;li&gt;Declare a constant &lt;code&gt;a&lt;/code&gt; to be &lt;code&gt;pow(sin(deltaPhi / 2), 2) + cos(phi1) * cos(phi2) * pow(sin(deltaLambda/2), 2)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Declare a constant &lt;code&gt;c&lt;/code&gt; to be &lt;code&gt;2 * atan2(sqrt(a), sqrt(1-a))&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Return &lt;code&gt;R * c / 1000.0&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;distanceKm(loc1: location1, loc2: location2)&lt;/code&gt; should be around &lt;code&gt;3511&lt;/code&gt; just like the web page.&lt;/p&gt;

&lt;p&gt;Now see if you can find the two closest places. Start with this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func closestTo(places: [[String: Any]], loc: (lat: Double, long: Double)) -&amp;gt; String {
    var smallestDistance = Double.greatestFiniteMagnitude
    var closest = ""

    for p in places {
        if
            let name = p["name"] as? String,
            let loc2 = p["location"] as? (lat: Double, long: Double),
            let distance = distanceKm(loc1: loc, loc2: loc2),
            distance &amp;lt; smallestDistance {
                smallestDistance = distance
                closest = name
        }
    }

    return closest
}

// Find the closest place to the Eiffel Tower (should be Buckingham Palace)
closestTo(places: places, loc: (lat: 48.8583736, long: 2.2922873))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this function as a start. You need to make sure you don't compare a place to itself, because that will always be the closest.&lt;/p&gt;

&lt;p&gt;DM me or comment below if you need help.&lt;/p&gt;

&lt;h3&gt;
  
  
  Next: Closures
&lt;/h3&gt;

&lt;p&gt;The next article will provide exercises for the &lt;a href="https://docs.swift.org/swift-book/LanguageGuide/Closures.html"&gt;Closures&lt;/a&gt; chapter. Read it before you move on.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>beginners</category>
    </item>
    <item>
      <title>iOS StackOverflow Deep Dive: How to get two VC's to talk to each other</title>
      <dc:creator>Lou Franco</dc:creator>
      <pubDate>Mon, 27 Jan 2020 00:33:09 +0000</pubDate>
      <link>https://dev.to/loufranco/ios-stackoverflow-deep-dive-how-to-get-two-vc-s-to-talk-to-each-other-528d</link>
      <guid>https://dev.to/loufranco/ios-stackoverflow-deep-dive-how-to-get-two-vc-s-to-talk-to-each-other-528d</guid>
      <description>&lt;p&gt;This is a new series where I will go into depth on interesting iOS StackOverflow questions:&lt;/p&gt;

&lt;p&gt;This one &lt;a href="https://stackoverflow.com/questions/59923590/how-to-pass-a-reference-between-two-unrelated-viewcontrollers-for-a-main-and-an"&gt;starts out&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What is the most “Swiftian” way of passing a reference between two unrelated ViewControllers (e.g., two ViewControllers that are independently instantiated at different times)?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The gist of the question is that there are two totally unrelated View Controllers that need to show the same information at the same time. The questioner wants to know how they can get references to each other.&lt;/p&gt;

&lt;p&gt;My suggestion: Remember that iOS's default architectural pattern is MVC -- or Model-View-Controller. Introducing a model makes this easy.&lt;/p&gt;

&lt;p&gt;Now, the default way of doing this (with a delegate) is actually not appropriate because the standard way of implementing that assumes that you have just one, but in this case, we have two (the two VCs).&lt;/p&gt;

&lt;p&gt;There are several ways you could go from here.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use &lt;code&gt;NotificationCenter&lt;/code&gt; and have the model publish onto it and the VC's subscribe to it&lt;/li&gt;
&lt;li&gt;Use KVO (Key-Value-Observing) on model properties. This is a simple way to go if the model is mostly just properties that change. This is more likely to be the case for simple data, but for this specific problem it probably won't work well.  One idea is to have an every increasing &lt;code&gt;Int&lt;/code&gt; value, and when you get a KVO update on it, you ask the model for it's current information. The model just does &lt;code&gt;+= 1&lt;/code&gt; on it when it updates itself.&lt;/li&gt;
&lt;li&gt;If you are on iOS13, use &lt;code&gt;Combine&lt;/code&gt; or (if not) consider using &lt;code&gt;RxSwift&lt;/code&gt;. Both of these implement the &lt;code&gt;Observer&lt;/code&gt; pattern and can let you do simple publish-subscribes.&lt;/li&gt;
&lt;li&gt;Going the delegate route is still possible, but it involves an array of delegates -- but you need to implement some kind of array that holds elements weakly.  See this: &lt;a href="https://www.objc.io/blog/2017/12/28/weak-arrays/"&gt;https://www.objc.io/blog/2017/12/28/weak-arrays/&lt;/a&gt;. It's more complex than it seems, but possible.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I'll go into more detail in a future article.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Swift Programming Language Companion: Review of Basics to Functions</title>
      <dc:creator>Lou Franco</dc:creator>
      <pubDate>Sun, 26 Jan 2020 19:56:05 +0000</pubDate>
      <link>https://dev.to/loufranco/the-swift-programming-language-companion-review-of-basics-to-functions-359</link>
      <guid>https://dev.to/loufranco/the-swift-programming-language-companion-review-of-basics-to-functions-359</guid>
      <description>&lt;p&gt;This article is part of a series on learning Swift by writing code to The Swift Programming Language book from Apple.&lt;/p&gt;

&lt;p&gt;We've covered the basics up to functions, and now I'll provide exercises that review topics from all of those chapters.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up a reading environment
&lt;/h3&gt;

&lt;p&gt;If you are jumping around these articles, make sure you read the &lt;a href="https://dev.to/loufranco/the-swift-programming-language-companion-introduction-4k0a"&gt;Introduction&lt;/a&gt; to see my recommendation for setting up a reading environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Review
&lt;/h3&gt;

&lt;p&gt;This is a good point to review because you actually know enough to make some interesting functions. It is not yet enough to jump into apps, because the iOS Frameworks use some of the more advanced Swift features that we haven't learned yet.&lt;/p&gt;

&lt;p&gt;At this point, I would recommend staying away from App Tutorials until we have more of the fundamentals.&lt;/p&gt;

&lt;p&gt;To recap, we learned about the basic types (numbers, strings, bools), the operations you could do on them, optionals, control flow, collection types (arrays, sets, and dictionaries), and functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  This is practice
&lt;/h3&gt;

&lt;p&gt;As a reminder -- if you are a coder in other languages, these exercises will seem very simple. You might be tempted to just look at them and think, "I know how to do this". And, maybe you do.&lt;/p&gt;

&lt;p&gt;But, just do them anyway in a Playground. The intent is to get automatic with Swift syntax so that you can write it as fast as you think without needing to look up simple things all of the time.&lt;/p&gt;

&lt;p&gt;If you already code, this won't take long. If these articles are your introduction to programming, then you should be doing these exercises more than once.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exercises
&lt;/h3&gt;

&lt;p&gt;For this chapter, we're going to write code inspired by the Music app.&lt;/p&gt;

&lt;p&gt;Copy this into your playground&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let songs: [[String: Any]] = [
    ["title": "Shake it off", "artist": "Taylor Swift", "year": 2014],
    ["title": "Java", "artist": "Al Hirt", "year": 1963],
    ["title": "Galaxy Song", "artist": "Monty Python", "year": 1983],
    ["title": "Oh, Mojave", "artist": "The Ruby Suns", "year": 2007],
    ["title": "Fast As You Can", "artist": "Fiona Apple", "year": 1999],
    ["title": "Keyboard", "artist": "Pascal", "year": 2018],
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In your Playground write code to do the following&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add some songs to the array. Follow the same key structure.&lt;/li&gt;
&lt;li&gt;Make a function that takes the songs as a parameter and returns the number of songs in the array.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Make a function that takes the songs as a parameter and an index number (n) and returns the Nth song in the array.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;func getSong(songs: [[String: Any]], n: Int) -&amp;gt; [String: Any]&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Make a function that takes a single song dictionary (&lt;code&gt;[String: Any]&lt;/code&gt;) and returns the title.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;func getTitle(song: [String: Any]) -&amp;gt; String?&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This return &lt;code&gt;String?&lt;/code&gt; because there is nothing enforcing that the title key is there or that it is a string. You can use &lt;code&gt;as? String&lt;/code&gt; on a variable to try to convert it. This will give you the string or nil.&lt;/p&gt;

&lt;p&gt;You should be able to call it like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;getTitle(song: songs[0])&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Make a function that takes a single song dictionary (&lt;code&gt;[String: Any]&lt;/code&gt;) and a year and returns &lt;code&gt;true&lt;/code&gt; if the song is from that year.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;func isSongInYear(song: [String: Any], year: Int) -&amp;gt; Bool&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When you get the year, if you get &lt;code&gt;nil&lt;/code&gt;, return &lt;code&gt;false&lt;/code&gt; from the function.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Make a function that takes the entire &lt;code&gt;songs&lt;/code&gt; array and a year and returns the songs from that year as an array of song dictionaries.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Make a function that takes the entire &lt;code&gt;songs&lt;/code&gt; array and two years and returns the songs that are between (and including) those years.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;func getSongsBetween(songs: [[String: Any]], startYear: Int, endYear: Int)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Call it like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;getSongsBetween(songs: songs, startYear: 1981, endYear: 2007)&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Next:
&lt;/h3&gt;

&lt;p&gt;We'll continue with more review of these chapters in the next article.&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Swift Programming Language Companion: Functions (Part 2)</title>
      <dc:creator>Lou Franco</dc:creator>
      <pubDate>Mon, 20 Jan 2020 01:16:57 +0000</pubDate>
      <link>https://dev.to/loufranco/the-swift-programming-language-companion-functions-part-2-329k</link>
      <guid>https://dev.to/loufranco/the-swift-programming-language-companion-functions-part-2-329k</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/loufranco/the-swift-programming-language-companion-functions-446j"&gt;last article&lt;/a&gt;, I gave some exercises for functions.  Here are some more to cover the more advanced features.&lt;/p&gt;

&lt;p&gt;For these exercises, we're going to use exercises imagined around a diet tracker. In your playground copy this code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let foodCalories = [
    "Apple": 72,
    "Bagel": 289,
    "Banana": 105,
    "Carrots": 52,
    "Egg": 102,
    "Butter": 102,
    "Cookie": 150,
    "Cola": 136,
    "Water": 0,
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let food = foodCalories.randomElement()
food?.key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Will tell you a random food from the list.  &lt;code&gt;food?.key&lt;/code&gt; is of type &lt;code&gt;String?&lt;/code&gt; because if &lt;code&gt;food&lt;/code&gt; is an empty dictionary, we won't be able to find a randomElement.&lt;/p&gt;

&lt;p&gt;This code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let firstFood = foodCalories.first
firstFood?.key

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

&lt;/div&gt;



&lt;p&gt;Will tell you the "first" food from the list, which is also kind of random (but should be the same each call), because dictionaries don't have an order.&lt;/p&gt;

&lt;p&gt;Now:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Make a function that takes the &lt;code&gt;foodCalories&lt;/code&gt; dictionary as a parameter and return a random food.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;func randomFood(foodCalories: [String: Int]) -&amp;gt; String?&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Take the first function, copy it, and change it so that you don't have to use a label for the first argument. Change it so that we can call it like &lt;code&gt;randomFood(foodCalories)&lt;/code&gt; instead of &lt;code&gt;randomFood(foodCalories: foodCalories)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Take the first function, copy it and change it so that you use &lt;code&gt;from&lt;/code&gt; as a label for the first argument. Change it so that we can call it like &lt;code&gt;randomFood(from: foodCalories)&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Make a function that takes the &lt;code&gt;foodCalories&lt;/code&gt; dictionary as a parameter and returns the "first" food.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;func firstFood(foodCalories: [String: Int]) -&amp;gt; String?&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This one is hard ... Make a function that takes a function of the type that &lt;code&gt;randomFood&lt;/code&gt; and &lt;code&gt;firstFood&lt;/code&gt; are, and calls it three times and returns a String array with the results of the three calls.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Try the last one. If you need help, read on ...&lt;/p&gt;

&lt;p&gt;The type is &lt;code&gt;([String: Int]) -&amp;gt; String?&lt;/code&gt;.  This means, "a function that takes a dictionary and returns an optional String”.  Both &lt;code&gt;randomFood&lt;/code&gt; and &lt;code&gt;firstFood&lt;/code&gt; match this type.  You can make a function that takes this type as a parameter.&lt;/p&gt;

&lt;p&gt;Try it ...&lt;/p&gt;

&lt;p&gt;Ok, here's the function declaration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func threeFoods(foodCalories: [String: Int], foodPicker: ([String: Int]) -&amp;gt; String?) -&amp;gt; [String] {
   return []
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can call this function like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;threeFoods(foodCalories: foodCalories, foodPicker: randomFood(foodCalories:))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This says -- give me three foods from the &lt;code&gt;foodCalories&lt;/code&gt; dictionary using the &lt;code&gt;randomFood&lt;/code&gt; function to pick them.&lt;/p&gt;

&lt;p&gt;Fill in the &lt;code&gt;threeFoods&lt;/code&gt; function so it does this. Inside that function, &lt;code&gt;foodPicker&lt;/code&gt; is a function you can call.  Functions can be passed to functions, just like Strings, Ints, etc.&lt;/p&gt;

&lt;p&gt;There are tricky things here regarding optionals and building the array.  DM me or comment below if you need help.&lt;/p&gt;

&lt;h3&gt;
  
  
  Next:
&lt;/h3&gt;

&lt;p&gt;Now that we know the basic types, collection types, control flow, and functions. For the next few chapters I will give exercises that review all of those topics before moving on.&lt;/p&gt;

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