<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Joyce Matos on Medium]]></title>
        <description><![CDATA[Stories by Joyce Matos on Medium]]></description>
        <link>https://medium.com/@JoyceMatos?source=rss-493d44b4dbc8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ElykEQDwgkpSV61KvITqfQ.jpeg</url>
            <title>Stories by Joyce Matos on Medium</title>
            <link>https://medium.com/@JoyceMatos?source=rss-493d44b4dbc8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 02:49:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@JoyceMatos/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Dependency Injection in Swift]]></title>
            <link>https://medium.com/@JoyceMatos/dependency-injection-in-swift-87c748a167be?source=rss-493d44b4dbc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/87c748a167be</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <dc:creator><![CDATA[Joyce Matos]]></dc:creator>
            <pubDate>Thu, 03 Aug 2017 15:59:06 GMT</pubDate>
            <atom:updated>2017-08-17T18:27:32.928Z</atom:updated>
            <content:encoded><![CDATA[<p>Dependency Injection is often used with the intention of writing code that is loosely coupled, and thus, easier to test. When we use dependency injection in our code, we are essentially giving an object its instance variables. What this means is that instead of giving our object the responsibility of creating it’s own dependencies, we inject the dependencies to the object instead.</p><p>Below are two working examples that demonstrate an object creating it’s own dependencies, and an object that will receive it’s dependencies by injection.</p><pre>class LibraryViewModel {</pre><pre>     var library = Library()</pre><pre>}</pre><p>In the code above we have a LibraryViewModel who carries and instantiates the property library, thus, creating it’s own dependency.</p><pre>class LibraryViewModel {</pre><pre>     var library = Library?</pre><pre>}</pre><pre>let libraryViewModel = LibraryViewModel()<br>let library = Library()<br>libraryViewModel.library = library</pre><p>Here have the same view model object, but now it is no longer responsible for creating its own dependency. In this particular example, the dependency is injected into the LibraryViewModel via the library property and it receiving it from the outside. This kind of dependency injection is called <strong>property injection</strong>.</p><p>Another way to implement dependency injection is through its constructor, or initializer. A <strong>constructor injection</strong> is another type of dependency injection where the dependency is injected into an object right at its inception — the initializer.</p><pre>class LibraryViewModel {</pre><pre>     var library: Library</pre><pre>     init(library: Library) {<br>          self.library = library<br>     }</pre><pre>}</pre><p>Here we see that our dependency is located in the constructor portion of the code and is responsible for initializing our entire LibraryViewModel through this one property.</p><p>In iOS, this type of injection is often preferred because it prevents us from using an object that isn’t fully configured or has any missing pieces.</p><p>Next we have <strong>method injection</strong>, and if you’re guessing that this is where we inject dependencies through our methods, you are correct! Here is an example of method injection at play.</p><pre>protocol LibraryManager {</pre><pre>     func deleteAllBooks(in library: Library)<br>}</pre><p>As you can see, our LibraryManager protocol has a method on it that takes in an injected dependency.</p><p>Now that we have seen the different ways that dependencies can be injected into our code, let’s go over some of the benefits of dependency injection.</p><h3>Benefits of Dependency Injection:</h3><h4>Separation of concerns</h4><p>Dependency injection allows us to understand our code in a much clearer way and separates our concerns. When we use dependency injection, we can see that our objects are responsible for managing and handling the given dependency. Following one of the examples above, our LibraryViewModel is responsible for managing and handling our library object.</p><h4>Testing</h4><p>Earlier in the post I mentioned that dependency injection is great for testing, and here’s why:</p><p>When we are testing our objects, we can easily set up unit tests and replace our dependency for another property of its type. This allows us to create mock objects and isolate the behavior in a way that allows us to understand the behavior of our code. With dependency injection, our class, or struct, becomes much more flexible in different contexts and we do not have to change any of its internal code.</p><p>Here is an example of dependency injection really shining through in our unit test.</p><pre>class LibraryManagerTests: XCTestCase {</pre><pre>     func test_GettingLibrary_ShouldReturnBooks() {<br>          var fakeLibraryViewModel = LibraryViewModel()<br>          var fakeLibrary = Library()<br>          fakeLibrary.title = &quot;My Fake Library&quot;</pre><pre>          fakeLibraryViewModel.library = fakeLibrary</pre><pre>          // Some assertion about our viewModel<br>     }</pre><pre>}</pre><p>Here I have created a test double and am injecting a fakeLibrary into our library property in our fakeLibraryViewModel. This helps me test the behavior of this object without having to use any additional frameworks.</p><p>If you have any examples of how you use dependency injection in conjunction with protocols, for the purpose of networking, or any other interesting ways, please comment below and share your code!</p><p>Thanks for reading :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87c748a167be" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hashable Protocols in Swift]]></title>
            <link>https://medium.com/@JoyceMatos/hashable-protocols-in-swift-baf0cabeaebd?source=rss-493d44b4dbc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/baf0cabeaebd</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Joyce Matos]]></dc:creator>
            <pubDate>Thu, 02 Mar 2017 15:38:12 GMT</pubDate>
            <atom:updated>2017-07-11T23:48:21.101Z</atom:updated>
            <content:encoded><![CDATA[<p>Yesterday I had a conversation about sets and the idea of types conforming to a hashable protocol came up. It didn’t take long for me realize that my understanding of hashable protocols were limited, so I started piecing together the information that I did know. I knew that a protocol is similar to a blueprint in that it lays out a number of responsibilities that one must adopt and conform to. I also knew that sets stored distinct values of the same type in an unordered collection. My guess is that when we are using a custom type in a set, it has to conform to hashable protocol, so that means that there are a number of requirements it needs to meet in order to be considered hashable. Either way, I couldn’t go in depth about what a hashable protocol actually was or how they were used in custom types, so I began to do some investigating.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/312/1*91dpN5lBiGdiS1Mr75_P2Q.jpeg" /></figure><p>I headed over to Apple’s documentation to try and figure out what a hashable protocol was and hopefully get a more complete picture of why they are used in sets.</p><p>Apple defines <strong>hashable</strong> as <em>“a type that provides an integer a hash value.”</em> Okay, but what’s a hash value?- With some more reading I found that a hash value is provided by a type’s hashValue property and it is basically an integer that is the same for any two instances that compare equally.</p><p>To sum that up, a hashable is a type that has hashValue in the form of an integer that can be compared across different types.</p><p>So let’s say for instance that we have two instances, a and b. If a == b, then a.hashValue == b.hashValue, however two instances with the same hashValue will not always be equal.</p><p>When we conform to a hashable protocol we must also conform to the equatable protocol, but let’s take this one step at a time.</p><p>We conform to a hashable protocol like we do any other protocol:</p><pre>class Dog: Hashable {</pre><pre>     var uid: Int<br>     var name: String<br>     var age: Int</pre><pre>     init(uid: Int, name: String, age: Int) {<br>          self.uid = uid<br>          self.name = name<br>          self.age = age<br>     }<br>}</pre><p>But when we conform to a hashable protocol we must have a hashValue property.</p><pre>class Dog: Hashable {</pre><pre>     var uid: Int<br>     var name: String<br>     var age: Int<br>     var hashValue: Int {<br>          return uid.hashValue<br>     }</pre><pre>init(uid, Int, name: String, age: Int) {<br>          self.uid = uid<br>          self.name = name<br>          self.age = age<br>     }<br>}</pre><p>If we leave our code this way you’ll notice an error: <em>“Type ‘Dog’ does not conform to protocol ‘Equatable’.”</em></p><p>Although we are conforming to Hashable, Hashable must also conform to Equatable . Apple definesEquatable as a type that can be compared for value equality, which if we remember, is part of the working definition for a hashable protocol.</p><p>To conform to Equatable we must add a function that compares the type and checks to see if they are equal.</p><pre>static func ==(lhs: Dog, rhs: Dog) -&gt; Bool {<br>     return lhs.uid == rhs.uid<br>}</pre><p>And just like that we can begin to use our custom types as key values for dictionaries and or elements in a set too!</p><pre>let dog1 = Dog(uid: 1, name: &quot;Scrappy&quot;, age: 4)<br>let dog2 = Dog(uid: 2, name: &quot;Sparky&quot;, age: 3)</pre><pre>var dogArray: [Dog: String] = [<br>     dog1: &quot;Woff Woff&quot;,<br>     dog2: &quot;Wooooooof!&quot;<br>     ]</pre><pre>var dogs: Set&lt;Dog&gt; = [dog1, dog2]</pre><p>You will also notice that is we compare each instance’s hashValue they are different even though they are of the same type.</p><pre>print(dog2.hashValue) <br>print(dog1.hashValue) </pre><pre><em>// prints 4792065050360486817<br>// prints 8918364877838387272</em></pre><p>To conclude, a hashable protocol allows us to create custom types that can be compared for it’s equality using it’s hashValue. In this way we are able to use our custom type in a set or as a key in a dictionary while ensuring that it has a unique value.</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=baf0cabeaebd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Today Extensions in iOS 10 and Swift 3]]></title>
            <link>https://medium.com/@JoyceMatos/today-extensions-in-ios-10-and-swift-3-f7dc316d9df9?source=rss-493d44b4dbc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7dc316d9df9</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios-10]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Joyce Matos]]></dc:creator>
            <pubDate>Mon, 20 Feb 2017 23:20:13 GMT</pubDate>
            <atom:updated>2017-02-20T23:20:13.291Z</atom:updated>
            <content:encoded><![CDATA[<p>The widgets that are found in the Today view of our iOS devices are called extensions. Not only are they a useful way to present information to our users, but they are easy to implement and keep our users engaged with our app, even when they are not open.</p><p>My interest in widgets came from an app that I am currently working on. Ignite - Be Inspired sends an inspirational message at the time of your choice to ignite your day every day. Creating an extension for this app is powerful because it allows users to keep their motivating message on display without having to open the app.</p><p>To create our today extension we will have to open up Xcode and go to <strong>File &gt; New &gt; Target &gt; Application Extension &gt; Today Extension</strong>.</p><p>Once this file has been created you will notice that there is now a designated target with it’s corresponding files, including a storyboard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/485/1*Hjfepovj5W_LI_0OR4v0Pw.png" /><figcaption>Storyboard for Today Extension</figcaption></figure><p>In the Storyboard file I created two labels that will display the text I’d like to include in the today extension. After doing so I hooked up an IBOutlet for the second label and named it quote since this will be the only label who’s text we will change.</p><p>In the viewDidLoad() method from the TodayViewController I added this line of code to give the quote label a default message.</p><pre>quote.text = &quot;Open your app to read your quote of the day.&quot;</pre><p>Upon opening the widget, the today extension will tell the user to open up the app to receive their quote of the day.</p><p>Since the purpose of using this widget is to display information from the app, we have to figure out a way to access the data in the app and make it available in the extension. To do this I used UserDefaults to share information and access them across the different targets.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*mDHDXqBbFTqHszXK62Yjpg.png" /><figcaption>App Group</figcaption></figure><p>Before we touch UserDefaults we have to create an <strong>App Group</strong> so that we can store the information that we want to share between the widget and our app. To do this we will have to go to our project and click on our app’s target. Then we will go to <strong>Capabilities &gt; App Groups &gt; ON</strong>. From here we will click on the <strong>+ </strong>sign and create an app group like the one I created above. <em>*Make sure to copy the name of this group since we will be creating another app group for our extension target.</em></p><p>Next we will go to our widget’s target and repeat the same exact steps using the same app group name.</p><p>From here we can begin using UserDefaults to share data. In our app I’ve added this line of code to store the information I’d like to use in my widget.</p><pre>UserDefaults.init(suiteName: &quot;group.com.joycematos.ignite&quot;)?.setValue(&quot;If you are not willing to risk the usual, you will have to settle for the ordinary.&quot;, forKey: &quot;testQuote&quot;)</pre><p>Here I have included the name of the app group<strong> </strong>and setting a value and key pair that can be used whenever this app group is invoked.</p><p>Next I will move on to my extension target and write a few lines of code describing the type of actions I’d like to take in the viewDidLoad() method.</p><pre>if let quoteFromApp = UserDefaults.init(suiteName: &quot;group.com.joycematos.ignite&quot;)?.value(forKey: &quot;testQuote&quot;) {<br>     quote.text = quoteFromApp as? String<br>}</pre><p>Here I am unwrapping the value that is stored inUserDefaults and setting the quote label to display the value that we just unwrapped. When our widget loads, it will display the quote that we stored in our app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*4kbk2018Awda7LWcTxZ3YQ.png" /></figure><p>Notice that our TodayViewController has a method called widgetPerformUpdate(completionHandler:). This is where our widget gets notified of any updates about our data.</p><p>In this method we will add these lines of code:</p><pre>if let quoteFromApp = UserDefaults.init(suiteName: &quot;group.com.joycematos.ignite&quot;)?.value(forKey: &quot;testQuote&quot;) {<br>     if quoteFromApp as? String != quote.text {<br>     quote.text = quoteFromApp as? String<br>     completionHandler(NCUpdateResult.newData)<br>          } else {<br>     completionHandler(NCUpdateResult.noData)<br>     } else {<br>     quote.text = &quot;Open your app to read your quote of the day.&quot;<br>     completionHandler(NCUpdateResult.newData)<br>}</pre><p>This may seem like a lot but I’ll explain it step by step. First we will unwrap the value from our UserDefaults</p><pre>if let quoteFromApp = UserDefaults.init(suiteName: &quot;group.com.joycematos.ignite&quot;)?.value(forKey: &quot;testQuote&quot;) {</pre><pre>// Do Something</pre><pre>} else {<br>     quote.text = &quot;Open your app to read your quote of the day.&quot;<br>     completionHandler(NCUpdateResult.newData)</pre><pre>}</pre><p>If we are able to successfully unwrap the value in our UserDefaults, we will perform some type of action. If we are not able to unwrap this value, it’s because nothing has been stored to UserDefaults and we have to open our app to retrieve that data.</p><p>If there is a value in UserDefaults that we can unwrap, we will do this</p><pre>if quoteFromApp as? String != quote.text {<br>     quote.text = quoteFromApp as? String<br>     completionHandler(NCUpdateResult.newData)<br>} else {<br>     completionHandler(NCUpdateResult.noData)<br>}</pre><p>We check to see if the value stored in our UserDefaults is the same as the text being displayed in our widget. If these values are not equal, this means that our app has updated and we will call on the completion to notify our widget of this update. However, if these values are equal, we will call on the completion and let our widget know that there is no update to be made.</p><p>And that’s it! That’s how you create a today extension and update it based on the information we receive from our app.</p><p>Thanks for reading and happy coding :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7dc316d9df9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using NSNotificationCenter in Swift]]></title>
            <link>https://medium.com/@JoyceMatos/using-nsnotificationcenter-in-swift-eb70cf0b60fc?source=rss-493d44b4dbc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb70cf0b60fc</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Joyce Matos]]></dc:creator>
            <pubDate>Mon, 13 Feb 2017 01:11:50 GMT</pubDate>
            <atom:updated>2017-02-13T01:11:50.463Z</atom:updated>
            <content:encoded><![CDATA[<p>NSNotificationCenter can be seen as a tool for communicating information within your app. Unlike push or local notifications where you are notifying a user of any content you would like them to receive, NSNotificationCenter allows us to send and receive information between classes and/or structs based on an action that has occurred in our app.</p><p>To put it simply, NSNotificationCenter can be thought of as a broadcaster and we can tune into different stations, or channels to listen for any changes.</p><p>NotificationCenter.default is where all notifications are posted to and are observed from. Each notification must have a unique way to identify themselves, which would then represent the channel we are tuning in to if we continue with our analogy. In this same way, if we were to observe, or listen, to any channel, we would call on the observe method available to us through NotificationCenter.default and perform some type of action based on this listening.</p><p>I’ve created a simple app that demonstrates the use of NSNotifcationCenter so that we can better understand what this would look like in code, and how it actually works. If you’re like me and you’re a hands on type of learner — feel free to code along.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*_dXhi_ttVNX2libtgUo7vA.png" /><figcaption>ChooseDestinationVC and DestinationVC</figcaption></figure><p>In this app I’ve embedded a navigation controller and hooked up any IBOutlets and IBActions needed. The goal of this app is to display the name of the destination chosen when it has been selected from a different view controller.</p><p>To begin we will start by creating actions that will be performed whenever NotificationCenter has detected a change within a given channel.</p><p>In our case, we are creating two functions that will change the city label in our ChooseDestinationVC.</p><pre>func setToPeru(notification: NSNotification) {</pre><pre>     cityChosenLabel.text = &quot;Peru&quot;</pre><pre>}</pre><pre>func setToArgentina(notfication: NSNotification) {</pre><pre>     cityChosenLabel.text = &quot;Argentina&quot;</pre><pre>}</pre><p>Each function will set the cityChosenLabel to it’s corresponding text. We won’t be doing anything with these functions yet, but soon we will call on them when we begin to observe in our app.</p><p>Remember when I mentioned that each notification should have a unique identity? Well, I’ve created an extension on Notification.Name and added two static properties with the name of each channel. Normally you can manually type in the string containing the name you would like to use for your notification when you call on the observe or post methods, but this helps us steer away from bugs and leaves less space for errors due to misspellings.</p><pre>extension Notification.Name {</pre><pre>     static let peru = Notification.Name(&quot;peru&quot;)<br>     static let argentina = Notification.Name(&quot;argentina&quot;)</pre><pre>}</pre><p>Afterwards we will add observation methods in our ViewDidLoad that will listen to their designated channels and perform actions based on each one.</p><pre>NotificationCenter.default.addObserver(self, selector: #selector(setToPeru(notification:)), name: .peru, object: nil)</pre><pre>NotificationCenter.default.addObserver(self, selector: #selector(setToArgentina(notfication:)), name: .argentina, object: nil)</pre><p>Our first observer method will perform the function we created above, setToPeru(notification:), where we change the label’s text in the event that a notification has been posted to the .peru channel. The same is then applied to the function that listens for and changes the label’s text to Argentina, setToArgentina(notification:).</p><p>From here we move on to our next view controller, DestinationVC. Here we will post a notification when someone clicks on either button.</p><pre>@IBAction func peruButton(_ sender: Any) {</pre><pre>     NotificationCenter.default.post(name: .peru, object: nil)</pre><pre>}</pre><pre>@IBAction func argentinaButton(_ sender: Any) {</pre><pre>     NotificationCenter.default.post(name: .argentina, object: nil</pre><pre>}</pre><p>We will post a notification to the .peru channel if a user clicks on the peruButton, and the same will apply for the argentinaButton with it’s respective channel.</p><p>Now we can run our app and see the text in our city label change depending on what button is clicked! Although this example was a simple one, there are so many ways of using NSNotificationCenter. This can be extremely useful when one has an app that requires a user to log in and would like to perform a certain action depending on whether or not they are logged in.</p><p>Feel free to leave a comment below and let me know how you plan to use NSNotificationCenter. I’d love to hear about what you all are up to and how this can be useful for your app!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb70cf0b60fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mix and Match: Objective-C and Swift]]></title>
            <link>https://medium.com/@JoyceMatos/mix-and-match-objective-c-and-swift-c6bceb7f81f3?source=rss-493d44b4dbc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/c6bceb7f81f3</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[objective-c]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Joyce Matos]]></dc:creator>
            <pubDate>Thu, 12 Jan 2017 18:08:25 GMT</pubDate>
            <atom:updated>2017-01-12T18:16:08.100Z</atom:updated>
            <content:encoded><![CDATA[<p>As I look to the iOS community and see so many people building apps in Swift, and sharing their knowledge, and learning experiences over the internet, I am inspired and excited about the future of this language. On the other hand, however, I still see a lot of apps that are built on Objective-C, or are in the process of migrating to Swift.</p><p>Whether the goal is to improve on existing apps by adding some functionality in Swift, or make a full transition into Swift, it is nonetheless, helpful to learn about the relationship between these two languages, and how they can coexist in a project.</p><p>With that in mind, I’ve explored the capabilities of having these two languages in one project, and what this may look like.</p><p>In Objective-C, there are two types of files, a .m file and .h file. A .m file is where methods are implemented, whereas the .h file is where we add our declarations and object interfaces.</p><p>Whenever we want to import a Swift file to an existing Objective-C project, or an Objective-C file into an existing Swift project, Xcode gives us the opportunity to create a bridging-header file which gives us access to the classes in the imported file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/573/1*vcsTq6Uj8409bZHjzRVbdQ.png" /><figcaption>Bridging Header File</figcaption></figure><p>Once we create a bridge-header, we can import all of the Objective-C classes we would like to interact with our Swift files in the same way classes are imported in Objective-C. If we want our Swift files to be exposed to these classes, however, we must import them into the bridge-header file.</p><pre>#import “House.h”<br>#import &quot;Book.h&quot;</pre><p>From there, we can then create instances of these classes in our Swift file and use them like so:</p><pre>class Swifty {<br>     let myHome = House()<br>     let favoriteBook = Book()<br>}</pre><p>Using Swift in Objective-C code, on the other hand, can be done in two ways. We can import a file, or we can declare a class forward. To import a class, one must go to <em>Build Settings -&gt; Packaging -&gt; Product Module Name</em>. From there we will get the name of the Product Module and import this to the Objective-C’s class file. The name of my file is called <em>ObjectiveCAndSwift</em>, so yours may be different.</p><pre>#import “ObjectiveCAndSwift-Swift.h”</pre><p>From here we can create instances of Swift objects in the same way we do when we are creating Objective-C objects.</p><pre>-(Swifty*)mySwiftObject;</pre><p>Another way to use a Swift object in Objective-C is by forward declaring a Swift class to reference it in an Objective-C interface. This is helpful in that it cuts down the compile time if we want to use this class in Objective-C’s header file. This is because we are not importing the actual file, just the part that we need from it.</p><pre>@class Swifty</pre><p>This is also useful for testing purposes, and avoids the risk of importing a file that already imports from another file, and may cause collision errors at runtime.</p><p>So your file may look like this:</p><pre>@class Swifty</pre><pre>@interface House: NSObject</pre><pre>@property (nonatomic, strong, readwrite) NSArray* content;</pre><pre>-(ObjcObject1 *)firstObject;<br>-(ObjcObject2 *)secondObject;<br>-(Swift *)mySwiftObject;</pre><pre>@end</pre><p>When we’re using Swift in Objective-C, Apple states:</p><blockquote>A Swift class must be a descendant of an Objective-C class to be accessible and usable in Objective-C.</blockquote><p>This is to say that classes with Swift-only features such as tuples, generics, etc. will not be compatible with Objective-C.</p><p>If you have any thoughts, questions, suggestions, or advice, feel free to leave a comment. I’m open to learning more about this topic and would be happy to hear what you have to say :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6bceb7f81f3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Data Structures in Swift: Queues and Stacks]]></title>
            <link>https://medium.com/@JoyceMatos/data-structures-in-swift-queues-and-stacks-e7d715634f07?source=rss-493d44b4dbc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/e7d715634f07</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Joyce Matos]]></dc:creator>
            <pubDate>Thu, 22 Dec 2016 17:35:11 GMT</pubDate>
            <atom:updated>2017-01-06T21:39:51.187Z</atom:updated>
            <content:encoded><![CDATA[<p>Two of the more common types of data structures are queues and stacks. Before learning about these data structures I had no idea that I was using them in my code all the time. If you’re not familiar with them yourself, then you probably have to0. Don’t worry, they’re not as scary as they sound.</p><p>To backtrack a bit, data structures are simply a way of organizing data. Arrays, sets, and dictionaries are all examples of data structures that store data in one place, however, they each have unique characteristics that differentiates one collection type from another.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*7acwp8sAq5qngOszyN2O0A.png" /></figure><p>Queues are similar to the three collections types mentioned above in that they are used to store a set of data, but are different in that the first item to go into this collection, will be the first item to be removed . This is also known as FIFO, first in first out. You can also think about this as a deli line where the first person to be on line will be the first person to be served.</p><p>Below we’ll create our own queue. I’ll be explaining the steps, but I suggest coding along as we go through each one. First, we’ll begin with a struct.</p><pre>struct Queue&lt;T&gt; {<br>     var list = [T]()<br>}</pre><p>In our struct we have a property called list that will store all of our data. For the purpose of this example I’ve given each property a generic type.</p><p>In this struct we will create two functions: one that enqueues (adds an element into our list), and one that dequeues, (removes elements from our list), like so:</p><pre>mutating func enqueue(_ element: T) {<br>      list.append(element)<br>}</pre><pre>mutating func dequeue() -&gt; T? {<br>     if !list.isEmpty {<br>       return list.removeFirst()<br>     } else {<br>       return nil<br>     }<br>}</pre><p>In our first function we are adding an item into our list, and in our second function, we are checking to see if the list is not empty before we proceed. If there are in fact items in our list, we will remove the first item and return it’s value.</p><p>Afterwards we will create a peek function.</p><pre>func peek() -&gt; T? {<br>     if !list.isEmpty {<br>          return list[0]<br>     } else {<br>       return nil<br>     }<br>}</pre><p>This function first checks to see if our list is empty, and if it is not, it will show us the first item in our list.</p><p>Lastly, we will create computed property that will tell us the status of our list.</p><pre>var isEmpty: Bool {<br>     return list.isEmpty<br>}</pre><p>That’s it! You’ve created a queue.</p><p>Following our definition, we can see that if we create an instance of a Queue and add items into our object, each element will be placed in the same order they have entered.</p><pre>var collection = Queue&lt;Int&gt;()</pre><pre>collection.enqueue(5)<br>collection.enqueue(12)</pre><pre><em>// prints: Queue&lt;Int&gt;(list: [5, 8, 12])</em></pre><p>Similarly if we were to remove an item from our queue, the first item that was put into our queue will be the first one out.</p><pre>collection.dequeue()</pre><pre><em>// prints: Queue&lt;Int&gt;(list: [8, 12])</em></pre><p>Try it out for yourself and test each function!</p><p>Similar to queues, stacks also store data, but they are different in that the order in which we remove each item does not follow the same rules as a queue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*bxlmY_031o1Nr_izYkd4Eg.png" /></figure><p>In stacks, we can visualize each item being placed on top of one another — much like how we would create a stack a books. The first item we put into our collection will be at the bottom, <em>or end</em>, of our stack. Like books, if we ever wanted to remove the first book we set down, we would have to remove each book that is on top first. In the case of stacks, the method it follows is LIFO: last in, first out.</p><p>Below we’ll create a stack and begin with a struct, just as we did before.</p><pre>struct Stack&lt;T&gt; {<br>     var array: [T] = []<br>}</pre><p>In our stack we’ve created a property called array that will store all of our data.</p><p>When adding elements to our stack, this is called push, and when we remove an element from our stack, this is called pop. Let’s create a function that will push an item into our array, and pop an item out of our array.</p><pre>mutating func push(_ element: T) {<br>     array.append(element)<br>}</pre><pre>mutating func pop() -&gt; T? {<br>     if !array.isEmpty {<br>         let index = array.count - 1<br>         let poppedValue = array.remove(at: index)</pre><pre>         return poppedValue<br>      } else {<br>         return nil<br>      }<br>}</pre><p>Our push function adds an item into our array, while our pop function checks to see if our array is not empty. If the array is not empty, we will then remove the last item of our array and return it’s value.</p><p>Similar to our queue, we will add a peek function which returns the last item in our array.</p><pre>func peek() -&gt; T? {<br>     if !array.isEmpty {<br>        return array.last<br>     } else {<br>        return nil <br>     }<br>}</pre><p>That’s it! You’ve just created a stack.</p><p>If you have any questions about these data structures, would like to chat about them, or have another way of implementing them — feel free to reach out!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e7d715634f07" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Error Handling in Swift 3: Try, Try?, and Try!]]></title>
            <link>https://medium.com/@JoyceMatos/error-handling-in-swift-3-try-try-and-try-f19705e32ff4?source=rss-493d44b4dbc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/f19705e32ff4</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[error-handling]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Joyce Matos]]></dc:creator>
            <pubDate>Wed, 09 Nov 2016 16:00:50 GMT</pubDate>
            <atom:updated>2016-11-09T16:03:17.786Z</atom:updated>
            <content:encoded><![CDATA[<p>Some of you may already be familiar with using the keyword try to handle errors in Swift. For those of you who do not know what try is, try is simply a way of dealing with a possible error in any given function. Often times we have functions that “throw”, meaning they have the ability to throw an error, and try is a neat keyword that lets us deal with the possibility of those errors.</p><p>When building our apps, there are three variations of try that we can use.</p><pre>try<br>try?<br>try!</pre><p>If you’re intimidated by these variants, don’t be! And if you’re unsure about which one to use don’t worry, I’ve got you covered.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/356/1*wktEHHms0MfF5WVqZAzt0Q.gif" /></figure><p>As I mentioned before, try is normally used in conjunction with a throw function, so we’ll talk a little about throw functions and see our try keywords in action.</p><p>Imagine we are trying to parse through some JSON so that we can bring this data into our application. There’s a possibility that this data may not reach our app because the internet may be down, or perhaps the data you are trying to pull from no longer exists! These are all situations that allow for possible errors as we try to retrieve data from our API’s. Swift however, has a built in function in it’s <strong>JSONSerialization</strong> method that is a throws by default. Swift knows that our attempt to retrieve data may bring some errors and has created a function that allows us to account for this scenario in the event that it does throw us an error.</p><p>When using try, our throw function has to be wrapped in a do-catch statement. This allows us to customize our error handling and perform specific actions based on the error.</p><p>Below we will use try to gracefully catch any errors.</p><pre>do {<br>   let responseJSON = try JSONSerialization.jsonObject(with: data,   options: []) as! [[String: Any]]</pre><pre>   completion(responseJSON)</pre><pre>   } catch {</pre><pre>   print(“Hm, something is wrong here. Try connecting to the wifi.“)</pre><pre>   }</pre><p>As developers, using try with a do-catch statement allows us to check for meaningful errors, handle them with style, and save our users from being frustrated. Think about it, do we really want our app to crash when our users don’t have any internet connection, or do we want to let them know that this app will continue to run as soon as they connect to wifi? In a scenario like this, a notification is much more helpful and our do-catch statement allows us to do just that.</p><p>On the flip-side, we can also handle our errors using try?. If you have any experience with optionals then you’ll soon realize that try? is just that. try? let’s us ignore our error and allow them to become nil whenever our function throws them. For this reason, we do not have to wrap our code in a do-catch statement. We do however, have to unwrap our variable if we are ever assigning a try? value to it.</p><pre>let responseJSON = try? JSONSerialization.jsonObject(with: data, options: []) as! [[String: Any]]</pre><pre>if let responseJSON = responseJSON {<br>   print(&quot;Yay! We have just unwrapped responseJSON!&quot;<br>}</pre><p>The code above assigns an optional value to <strong>responseJSON</strong> and allows it to become nil if the function called throws any errors. Using try? is helpful when the error that may be thrown is of little importance and we want to avoid assigning any actions to the error that was thrown.</p><p>Being that our <strong>responseJSON</strong> will be an optional we can integrate a guard statement into our code instead and just unwrap it in a readable manner.</p><pre>guard let responseJSON = try? JSONSerialization.jsonObject(with: data, options: []) as! [[String: Any]] else { return }</pre><p>Lastly we have try!, which operates like a forced-unwrapped optional. This variant basically makes the assertion that although the function has the ability to throw an error, this will never happen because the information we are trying to receive is guaranteed to be there. In other words, try! is used when we know there is no possible way our method will fail, or if this line of code is so fundamental that our whole app might as well crash if there is in fact an error.</p><pre>let responseJSON = try! JSONSerialization.jsonObject(with: data, options: []) as! [[String: Any]]</pre><p>Like all values that are forced-unwrapped, try! should be used carefully.</p><h4>In Conclusion</h4><ul><li>try is used with a do-catch statement and allows for a more detailed approach to error handling.</li><li>try? ignores our errors and will set them to nil if they happen to occur.</li><li>try! force unwraps your code and guarantees that our function will never encounter an error. In the case that our function does throw an error our app will simply crash.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f19705e32ff4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ARC: Strong and Weak References in Swift]]></title>
            <link>https://medium.com/@JoyceMatos/arc-strong-and-weak-references-in-swift-f2a085a17119?source=rss-493d44b4dbc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/f2a085a17119</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Joyce Matos]]></dc:creator>
            <pubDate>Tue, 25 Oct 2016 17:14:27 GMT</pubDate>
            <atom:updated>2016-10-27T18:53:09.436Z</atom:updated>
            <content:encoded><![CDATA[<p>My interest in blogging about references started with IBOutlets. I’ve always wondered why an IBOutlet was weak, or what this even meant. Weak? I’d asked myself, what is a weak variable?</p><p>With this question in mind I headed on over to Apple’s documentation to find out what this meant. Little did I know that weak was part of a topic that is more complex than I would have imagined.</p><p>What I found was that Apple uses Automatic Reference Counting, (ARC) to manage and track memory usage on an app. Using references like strong, weak, or unowned, are ways of letting ARC know when to free memory for any given object. In this post I will be focusing more on strong and weak references, as unowned will be saved for a later post.</p><h4>Let’s Begin With Strong</h4><p>A lot of us use strong references in our code without even realizing it. In fact, each time we declare a class property it defaults to a strong reference unless we specify otherwise.</p><pre>var name = String()</pre><p>Here’s a visual representation of what a strong reference cycle may look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/664/1*vcxS3Q1sR1ylrb1a7308dw.png" /><figcaption>Strong Reference Cycle</figcaption></figure><p>In essence, strong references are used to describe the relationship between objects. When an object has a strong reference to another object, this creates a retain cycle that prevents the object that is being referenced from being deallocated and increases the retain count to 1. In other words, these objects are now keeping each other alive.</p><p>Below I’ve created two classes, one of type <strong>Person</strong> and one of type <strong>Pet</strong>, with a strong reference cycle so that we can get an idea of what this may look like in code.</p><pre>class Person {</pre><pre>     var name: String</pre><pre>     var pet: Pet?<br></pre><pre>     init(name: String, pet: Pet?) {</pre><pre>        self.name = name</pre><pre>        self.pet = pet</pre><pre>     }<br>}</pre><pre><br>class Pet {</pre><pre>     var owner: Person?</pre><pre>     var pet: String<br></pre><pre>     init(owner: Person, pet: String) {</pre><pre>        self.owner = owner</pre><pre>        self.pet = pet</pre><pre>     }<br>}</pre><p>In this example, <strong>Person</strong> has a strong reference to <strong>Pet</strong> through it’s <strong>pet</strong> property, and <strong>Pet</strong> has a strong reference to <strong>Person</strong> through it’s <strong>owner</strong> property. The problem with this example is that because these objects hold strong references to one another, ARC is unable to deallocate memory when one of the objects are not needed.</p><p>In theory, if <strong>Pet</strong> was nil at any point, <strong>Person</strong> will still hold a strong reference to <strong>Pet</strong> and the memory for this class cannot be deallocated despite the fact that it is nil. In turn, if <strong>Person</strong> is nil at any point, ARC will not be able to free any memory for this object either.</p><p>To prevent this retain cycle we can create properties that hold a weak reference instead.</p><h4>Weak References</h4><p>Weak references are created using the keyword weak before its declaration.</p><pre>weak var name: String?</pre><p>Unlike strong references, weak references have a retain count of zero because the object that is being referenced can hold a missing value at some point and is subject to being deallocated by ARC. The relationship between the objects would look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*30sIvXWCRRZlTuCP5rZgYQ.png" /><figcaption>Weak Reference Cycle</figcaption></figure><p>Following the example above, we can change a property from within a class so that it holds a weak reference to another class like so:</p><pre>class Person {</pre><pre>     var name: String</pre><pre>     weak var pet: Pet?<br></pre><pre>     init(name: String, pet: Pet?) {</pre><pre>        self.name = name</pre><pre>        self.pet = pet</pre><pre>     }<br>}</pre><pre>class Pet {</pre><pre>     var owner: Person?</pre><pre>     var pet: String<br></pre><pre>init(owner: Person?, pet: String?) {</pre><pre>     self.owner = owner</pre><pre>     self.pet = pet</pre><pre>     }<br>}</pre><p>In relation to our code, the <strong>Pet</strong> class has a strong reference to the <strong>Person</strong> class, but the <strong>Person</strong> class has a weak reference to the <strong>Pet</strong> class.</p><p>When we have a property that holds a weak reference to an object, it’s type will be an optional since it has the ability to become nil at runtime. This means that if <strong>Pet</strong> becomes nil at any point, the <strong>Person</strong> class will release the memory stored for this type in it’s <strong>pet</strong> property.</p><p>With that in mind, a weak reference can never be a constant because it needs to be able to mutate.</p><h4>When should we use strong and weak references?</h4><p>Strong and weak references should be taken into consideration when we are creating reference types such as classes. Structs and Enums are value types so using any of the ARC keywords would not be applicable in these cases.</p><p><strong>Strong</strong> references should be used when a parent object is referencing a child object and never the other way around. That is, a child class should not have a strong reference to the parent class.</p><p><strong>Weak</strong> references should be used to avoid retain cycles and an object has the possibility to become nil at any point of it’s lifetime.</p><p>So now that we’ve covered some basic points on references and how they relate to memory management, we can now keep these details in mind when we are working on large projects with multiples files and classes.</p><p>Hope you all enjoyed!</p><p><strong>Helpful Links</strong></p><p><a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html">Apple Documentation — Automatic Reference Counting</a></p><p><a href="http://krakendev.io/blog/weak-and-unowned-references-in-swift">“Weak, Strong, Unowned, Oh My!” — A Guide To References In Swift</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f2a085a17119" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploring The Power of Sets — Swift 3.0]]></title>
            <link>https://medium.com/@JoyceMatos/exploring-the-power-of-sets-swift-3-0-8eabfc2ffd4e?source=rss-493d44b4dbc8------2</link>
            <guid isPermaLink="false">https://medium.com/p/8eabfc2ffd4e</guid>
            <category><![CDATA[swift-3]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Joyce Matos]]></dc:creator>
            <pubDate>Tue, 11 Oct 2016 01:35:37 GMT</pubDate>
            <atom:updated>2016-10-11T01:36:00.544Z</atom:updated>
            <content:encoded><![CDATA[<p>The three primary collection types in Swift are arrays, dictionaries, and sets. Those of you who have some experience using arrays and dictionaries have come across both the power and limitations of using each. In this post I will be focusing on sets for it’s ability to gather information from our collection in an easy and efficient way.</p><h4>What Are sets?</h4><p>Sets are a collection type in Swift that store distinct values of the same type in an unordered collection. Keyword: distinct. Similar to Swift’s other two collection types, arrays and dictionaries, sets are useful for storing and organizing data in an efficient way while maintaining its unique characteristics that allow for certain types of functionality.</p><p>When declaring a set, the type must be hashable, meaning that if you are using a custom type, it must conform to the Hashable protocol. Types like String, Int, Double, and Bool are already hashable by default so they do not need any modification.</p><p>So how do you create a set you may ask? Easy!</p><p>First lets start with the syntax. When creating a Swift type set, the proper syntax is Set&lt;Element&gt; . Element represents the type that will be stored in the set.</p><p>Here is an example of how we can initialize and create a set:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/852/1*drVGNSM_cgXV2goZ-B-y1g.png" /></figure><p><em>It is important to note that the Swift compiler cannot infer a type set as it does with an array. This means that the type should always be included when creating the instance of a set.</em></p><p>Since part of the working definition for sets include their ability to only store unique values, we can compare what our code would look like when adding duplicate values into an array vs. a set.</p><p>An array can hold multiple values and doesn’t concern itself with distinct values like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/1*rSR8Bm8FSwCfa1uNfSuh0A.png" /></figure><p>A set on the other hand highlights the importance of holding unique values and will not add a value if it is not distinct.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*l-KkrRzHO7yvgyVRJz2Ztg.png" /></figure><p>Now picture yourself trying to populate a collection type with a method that generates random values, and you don’t want duplicates. Wouldn’t a set be a convenient way of combating this problem?</p><p>In addition to storing unique values, sets can also perform a series of operations that are specific to this type.</p><p>This includes:</p><pre>.intersection(_:)<br>.symmetricDifference(_:)<br>.union(_:)<br>.subtracting(_:)</pre><p>Now suppose we were having guests over for dinner and we decided to bake a cake and brownies for dessert. Here is some working code illustrating these operations in action.</p><p>We can perform the .intersection(_:) method to create a set containing the values that are found in both sets, like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*7EDMZCUROIK1pK1l2o-8pA.png" /></figure><p>The .symmetricDifference(_:) method creates a set that holds the values that are unique to each set.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/516/1*sgC-PqC0j0N67B6vOZk-1A.png" /></figure><p>The .union(_:) method creates a set that combines the values in both sets without storing any duplicates.</p><p>If we wanted to create a shopping list of the ingredients needed for both our cake and brownies, our code would look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/861/1*wuf_4HJ-RqZ5hE3wT2KlWA.png" /></figure><p>Lastly, the .subtracting(_:) method creates a set with values that are not in the specified set.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*ttwV5CJEwMlHxJxg-LX48Q.png" /></figure><h4>Membership and Equality</h4><p>In addition to performing the operations mentioned above we are also able to check set membership and equality. Apple’s documentation describes what this means in the diagram below:</p><blockquote>The illustration below depicts three sets — a, b and c—with overlapping regions representing elements shared among sets. Set a is a <em>superset</em> of set b, because a contains all elements in b. Conversely, set b is a <em>subset</em> of set a, because all elements in b are also contained by a. Set b and set c are <em>disjoint</em> with one another, because they share no elements in common.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z757h0b5zKbwF4UXajwG1Q.png" /><figcaption>The Swift Programming Language (Swift 3) — Set Membership and Equality</figcaption></figure><p>Following Apple’s example on the documentation, lets say we have the following sets, can anyone guess which is the superset and subset?</p><pre>let houseAnimals: Set = [&quot;dog&quot;, &quot;cat&quot;]<br>let farmAnimals: Set = [&quot;cow&quot;, &quot;chicken&quot;, &quot;sheep&quot;, &quot;dog&quot;, &quot;cat&quot;]<br>let cityAnimals: Set = [&quot;pigeon&quot;, &quot;mouse&quot;]</pre><p>If you can’t, don’t worry, there’s a method for that!</p><p>The methods that are associated with set membership and equality are:</p><pre>==<br>.isSubset(of:)<br>.isSuperset(of:)<br>.isStrictSubset(of:) or isStrictSuperset(of:)<br>.isDisjoint(with:)</pre><p>What makes these methods different from the operations mentioned above is that they return a Bool statement describing the status of each set.</p><p>For example, if we wanted to check if two sets were equal we would use the == operator.</p><p>Or, if we wanted to check if farmAnimals was a superset of houseAnimals, we would simply write this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*4qbTphQe6tVJSHlP-2dX0Q.png" /></figure><p>Similarly, the .isSubset(of:) method would tell us whether all the values in the set are contained in a specified set. An example of this would be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/343/1*nHELEY1gV1-JaSINPusLzQ.png" /></figure><p>Like the .isSuperset(of:) and .isSubset(of:) method, the isStrictSubset(of:) and isStrictSuperset(of:) tells us whether a set is a superset or subset, but only if it is not equal to the specified set.</p><p>Lastly, the .isDisjoint(with:) determines whether the two sets have values in common.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*AzrZ2GNRTzaYnc1mnWOBFA.png" /></figure><p><strong>Conclusion</strong></p><p>Sets can be a great tool if you’re looking to work with distinct values and don’t care about the order of the collection. Not to mention that the operations and methods that are accessible to sets can save you tons of lines of code relative to using a different type of collection method to gather specific information about your values.</p><p>Don’t believe me? Try it out yourself! You have all the tools to get started, and if you want some extra information, take a look at the resources down below :)</p><h4>Resources</h4><p><a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html">Swift Documentation</a></p><p><a href="http://stackoverflow.com/documentation/swift/371/sets#t=201610081528402257493">Stack Overflow</a></p><p><a href="http://www.codingexplorer.com/swift-set-type/">Coding Explorer Blog — Swift Set Type</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8eabfc2ffd4e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>