<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><generator uri="https://jekyllrb.com/" version="3.9.0">Jekyll</generator><link href="https://pedrommcarrasco.github.io/atom.xml" rel="self" type="application/atom+xml" /><link href="https://pedrommcarrasco.github.io/" rel="alternate" type="text/html" hreflang="en-US" /><updated>2021-10-22T15:50:19+00:00</updated><id>https://pedrommcarrasco.github.io/atom.xml</id><title type="html">Pedro Carrasco</title><subtitle>Welcome to Pedro Carrasco's website</subtitle><author><name>Pedro Carrasco</name></author><entry><title type="html">How To Have Multiple Icons?</title><link href="https://pedrommcarrasco.github.io/posts/how-to-have-multiple-icons/" rel="alternate" type="text/html" title="How To Have Multiple Icons?" /><published>2019-06-30T00:00:00+00:00</published><updated>2019-06-30T00:00:00+00:00</updated><id>https://pedrommcarrasco.github.io/posts/how-to-have-multiple-icons</id><content type="html" xml:base="https://pedrommcarrasco.github.io/posts/how-to-have-multiple-icons/">&lt;p&gt;There are multiple ways to make your application feel more personal to your users or even reward the most loyal ones. However, there aren’t many as simple as having multiple application icons available and, in this post, you’ll learn how you can do it.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;“How to …  ?”&lt;/strong&gt; is a collection of short posts where I’ll share problems I’ve faced and how I ended up solving them.&lt;/p&gt;

  &lt;p&gt;tl;dr - Question 👉 Answer&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;problem&quot;&gt;Problem&lt;/h2&gt;

&lt;p&gt;While adding your main application icon is as simple as adding some image to an asset catalogue (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.xcassets&lt;/code&gt;), adding an alternative application icon won’t be anything like that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;⚠️ Spoiler:&lt;/strong&gt; You won’t even add it to an asset catalog 😱&lt;/p&gt;

&lt;h2 id=&quot;solution&quot;&gt;Solution&lt;/h2&gt;

&lt;p&gt;Assuming you have already designed a beautiful default icon and some alternatives, export them using the @2x and @3x naming convention and place them somewhere in your project inside a group (outside of any asset catalogue).&lt;/p&gt;

&lt;p&gt;In the end, you should have something as follows:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/pedrommcarrasco/pedrommcarrasco.github.io/blob/master/assets/posts/how-to-have-multiple-icons/folderExample.png?raw=true&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now, open your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Info.plist&lt;/code&gt; as &lt;a href=&quot;https://github.com/pedrommcarrasco/pedrommcarrasco.github.io/blob/master/assets/posts/how-to-have-multiple-icons/openAsSource.jpg?raw=true&quot;&gt;source code&lt;/a&gt;. At the end of your file, you should see &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;/dict&amp;gt;&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;/plist&amp;gt;&lt;/code&gt;. Paste the following before them:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/7ea615cf3755d3d42df4357a5e44721a.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;In case you didn’t notice, you must omit the file extension and the scaling suffix (@2x and @3x) from each entry.&lt;/p&gt;

&lt;p&gt;Once you’re done, you can freely change your application’s icon by calling &lt;a href=&quot;https://developer.apple.com/reference/uikit/uiapplication/2806818-setalternateiconname&quot;&gt;setAlternateIconName(_:completionHandler:)&lt;/a&gt; like follows:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/dabebe009c6738a6cb6c0ae774a6ef13.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Before ending, if your application also supports &lt;strong&gt;iPad&lt;/strong&gt;, you’ll need to add another dictionary to your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Info.plist&lt;/code&gt;. This dictionary should be a copy of the previously added one but with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~ipad&lt;/code&gt; suffix. Considering the previous dictionary, it would result in:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/19ade32e54e8870ac9988e827099fcbc.js&quot;&gt;&lt;/script&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Alternate application icons were introduced in iOS 10.3 and can help you make your application feel more personal to your users. While providing this feature isn’t difficult, it isn’t as easy as it could and should be and tools like &lt;a href=&quot;https://github.com/alexaubry/alternate-icons&quot;&gt;AlternativeIcons&lt;/a&gt; can improve your experience with it.&lt;/p&gt;

&lt;p&gt;Last but not least, if you have any subject you would like to see covered and/or discussed, let me know here or on &lt;a href=&quot;https://twitter.com/pedrommcarrasco&quot;&gt;Twitter&lt;/a&gt;! 👀&lt;/p&gt;

&lt;p&gt;Thanks for reading. ✨&lt;/p&gt;</content><author><name>Pedro Carrasco</name></author><category term="how to" /><summary type="html">There are multiple ways to make your application feel more personal to your users or even reward the most loyal ones. However, there aren’t many as simple as having multiple application icons available and, in this post, you’ll learn how you can do it.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://pedrommcarrasco.github.io/assets/posts/how-to-have-multiple-icons/banner.jpg" /><media:content medium="image" url="https://pedrommcarrasco.github.io/assets/posts/how-to-have-multiple-icons/banner.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">How To Display Your Dependencies?</title><link href="https://pedrommcarrasco.github.io/posts/how-to-display-your-dependencies/" rel="alternate" type="text/html" title="How To Display Your Dependencies?" /><published>2019-06-19T00:00:00+00:00</published><updated>2019-06-19T00:00:00+00:00</updated><id>https://pedrommcarrasco.github.io/posts/how-to-display-your-dependencies</id><content type="html" xml:base="https://pedrommcarrasco.github.io/posts/how-to-display-your-dependencies/">&lt;p&gt;Ever wanted to show your users which dependencies your application has? I needed this for an application I’ll launch soon and, in this post, you’ll learn how you can achieve it.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;“How to …  ?”&lt;/strong&gt; is a collection of short posts where I’ll share problems I’ve faced and how I ended up solving them.&lt;/p&gt;

  &lt;p&gt;tl;dr - Question 👉 Answer&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;problem&quot;&gt;Problem&lt;/h2&gt;

&lt;p&gt;Since I was using &lt;a href=&quot;[https://cocoapods.org](https://cocoapods.org/)&quot;&gt;CocoaPods&lt;/a&gt; to manage my dependencies, there was only one requirement: &lt;strong&gt;the data must be synchronized with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;podfile&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;solution&quot;&gt;Solution&lt;/h2&gt;

&lt;p&gt;While I’ve never heard of it before, there was a clear and “official” solution. It was the plugin &lt;a href=&quot;https://github.com/CocoaPods/cocoapods-acknowledgements&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cocoapods-acknowledgements&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This plugin generates a property list (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.plist&lt;/code&gt;) with the metadata of your dependencies. In order to use it, follow the next steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Install it via ruby with  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gem install cocoapods-acknowledgements&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plugin 'cocoapods-acknowledgements'&lt;/code&gt; to your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;podfile&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;You’ll now find &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pods-YOUR_TARGET-metadata.plist&lt;/code&gt; inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pods/&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With this property list you can either decode it with the following code and map/transform to your own user interface:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/cbe1665e7c921c107e7bd74306691289.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Or, if you rather have it in your application’s settings, you can also embed it in your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Settings.bundle&lt;/code&gt;. To do so, you just need to add  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plugin 'cocoapods-acknowledgements', :settings_bundle =&amp;gt; true&lt;/code&gt; to your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;podfile&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;You’re now able to display your dependendencies to your users and keep in mind that CocoaPods Acknowledgements is well documented and offers other features, such as excluding dependencies or even adding non-CocoaPods dependencies to your property list. You can find it all &lt;a href=&quot;https://github.com/CocoaPods/cocoapods-acknowledgements/blob/master/README.md&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Last but not least, if you have any subject you would like to see covered and/or discussed, let me know here or on &lt;a href=&quot;https://twitter.com/pedrommcarrasco&quot;&gt;Twitter&lt;/a&gt;! 👀&lt;/p&gt;

&lt;p&gt;Thanks for reading. ✨&lt;/p&gt;</content><author><name>Pedro Carrasco</name></author><category term="how to" /><summary type="html">Ever wanted to show your users which dependencies your application has? I needed this for an application I’ll launch soon and, in this post, you’ll learn how you can achieve it.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://pedrommcarrasco.github.io/assets/posts/how-to-display-your-dependencies/banner.jpg" /><media:content medium="image" url="https://pedrommcarrasco.github.io/assets/posts/how-to-display-your-dependencies/banner.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">How To Be Lazy In Extensions?</title><link href="https://pedrommcarrasco.github.io/posts/how-to-be-lazy-in-extensions/" rel="alternate" type="text/html" title="How To Be Lazy In Extensions?" /><published>2019-03-29T00:00:00+00:00</published><updated>2019-03-29T00:00:00+00:00</updated><id>https://pedrommcarrasco.github.io/posts/how-to-be-lazy-in-extensions</id><content type="html" xml:base="https://pedrommcarrasco.github.io/posts/how-to-be-lazy-in-extensions/">&lt;p&gt;How to extend an object with a lazy property? Recently, I needed this for &lt;a href=&quot;https://github.com/pedrommcarrasco/Constrictor&quot;&gt;Constrictor&lt;/a&gt; and, in this post, you’ll learn how you can achieve it.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;“How to …  ?”&lt;/strong&gt; is a collection of short posts where I’ll share problems I’ve faced and how I ended up solving them.&lt;/p&gt;

  &lt;p&gt;tl;dr - Question 👉 Answer&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;problem&quot;&gt;Problem&lt;/h2&gt;

&lt;p&gt;Let’s start with the obvious solution, which in this case, would be extending &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIView&lt;/code&gt; with:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/d017bf6c192fc788ec739384fcd394a7.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Clean and simple, but it doesn’t work… If you give it a try, Xcode will fail to compile with “&lt;strong&gt;Extensions must not contain stored properties&lt;/strong&gt;”.&lt;/p&gt;

&lt;h2 id=&quot;solution&quot;&gt;Solution&lt;/h2&gt;

&lt;p&gt;While potentially dangerous, &lt;strong&gt;Associated Objects&lt;/strong&gt; can still be very useful as they allow us to add new properties inside an extension of an existing object.&lt;/p&gt;

&lt;p&gt;As an example, we are able to create a lazy property in an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIView&lt;/code&gt; extension as follows:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/34c6f225f9c0285351fdd41cd7a20dc1.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;In short, we have a lazy property by associating a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Constrictor&lt;/code&gt;’s instance to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIView&lt;/code&gt;’s instance in runtime. If there is one associated, it will retrieve it. If there isn’t, it will create one and save its “address” in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AssociatedKey.constrictor&lt;/code&gt; so it can be retrieved in future calls.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;As mentioned, &lt;strong&gt;Associated Objects&lt;/strong&gt; can be dangerous if used improperly. To be fully aware of its capabilities and common pitfalls, I recommend reading Mattt’s &lt;a href=&quot;https://nshipster.com/associated-objects/&quot;&gt;article&lt;/a&gt; from NSHipster.&lt;/p&gt;

&lt;p&gt;Last but not least, if you have any subject you would like to see covered and/or discussed, let me know here or on &lt;a href=&quot;https://twitter.com/pedrommcarrasco&quot;&gt;Twitter&lt;/a&gt;! 👀&lt;/p&gt;

&lt;p&gt;Thanks for reading. ✨&lt;/p&gt;</content><author><name>Pedro Carrasco</name></author><category term="how to" /><category term="language feature" /><summary type="html">How to extend an object with a lazy property? Recently, I needed this for Constrictor and, in this post, you’ll learn how you can achieve it.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://pedrommcarrasco.github.io/assets/posts/how-to-be-lazy-in-extensions/banner.png" /><media:content medium="image" url="https://pedrommcarrasco.github.io/assets/posts/how-to-be-lazy-in-extensions/banner.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Let’s Talk About Double Standards</title><link href="https://pedrommcarrasco.github.io/posts/lets-talk-about-double-standards/" rel="alternate" type="text/html" title="Let’s Talk About Double Standards" /><published>2019-01-08T00:00:00+00:00</published><updated>2019-01-08T00:00:00+00:00</updated><id>https://pedrommcarrasco.github.io/posts/lets-talk-about-double-standards</id><content type="html" xml:base="https://pedrommcarrasco.github.io/posts/lets-talk-about-double-standards/">&lt;p&gt;Have you ever found yourself surrounded in a codebase where code guidelines seem to be ignored in its tests? It’s time to talk about this with &lt;a href=&quot;https://twitter.com/ZeMiguelFig&quot;&gt;José Figueiredo&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/twostraws&quot;&gt;Paul Hudson&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/rlrg_6&quot;&gt;Rodrigo López-Romero&lt;/a&gt; and &lt;a href=&quot;https://twitter.com/martinho_t&quot;&gt;Tiago Martinho&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;“Let’s talk about”&lt;/strong&gt; is a series where I discuss and showcase &lt;strong&gt;my point of view&lt;/strong&gt; about specific topics. However, as most topics can be highly opinionated, I will also &lt;strong&gt;bring other people’s opinion&lt;/strong&gt;.&lt;/p&gt;

  &lt;p&gt;tl;dr - Friendly Rant&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;Let’s start with the definition of &lt;em&gt;guideline&lt;/em&gt; according to the &lt;a href=&quot;https://dictionary.cambridge.org/dictionary/english/guideline&quot;&gt;Cambridge Dictionary&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;guideline&lt;/strong&gt; &lt;em&gt;(noun)&lt;/em&gt;&lt;/p&gt;

  &lt;p&gt;&lt;em&gt;information intended to advise people on how something should be done or what something should be&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We have lots of guidelines in our lives and people soon recognised we also needed them in programming. Otherwise, our codebases would be a combination of personal tastes embedded into an incoherent pile of computer instructions.&lt;/p&gt;

&lt;p&gt;Subsequently, we developed multiple guidelines from styling to best practices. On top of that, we even automated the review process with tools such as &lt;a href=&quot;https://github.com/realm/SwiftLint&quot;&gt;SwiftLint&lt;/a&gt; but what about tests?&lt;/p&gt;

&lt;p&gt;We follow a whole bunch of principles, patterns and guidelines but we often see tests ignoring them. Copy-pasting across multiple tests, response’s stubs polluting test files, magic numbers everywhere and force-unwrapping because “it is just a unit test”? What about having double standards when it comes to code guidelines?&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/twostraws&quot;&gt;Paul Hudson&lt;/a&gt;, author of &lt;a href=&quot;https://www.hackingwithswift.com&quot;&gt;Hacking with Swift&lt;/a&gt; and &lt;a href=&quot;https://www.hackingwithswift.com/store/testing-swift&quot;&gt;Testing Swift&lt;/a&gt;, agrees that tests are as important as application’s code and we shouldn’t have different standards between both:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;“The Swift language provides us with a dazzling array of language features and design patterns to help us create smart, safe, reusable code, but for some reason these get completely ignored when writing tests – we just write them the brute force way, with a thousand functions named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;testXYZ()&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

  &lt;p&gt;&lt;em&gt;At WWDC17, Xcode engineer Greg Tracy said “Treat your test code with the same amount of care as your app code.” Simple, but straight to the point: all the coding principles we apply in our application’s code should also be applied in our tests&lt;/em&gt;&lt;/p&gt;

  &lt;p&gt;&lt;em&gt;Many companies will talk proudly about their approach to code review – they might do pair programming, they might do GitHub reviews, they might have in-person code review sessions. But if you ask whether they do that same code review for their tests, you’ll find a problem: they usually don’t.&lt;/em&gt;&lt;/p&gt;

  &lt;p&gt;&lt;em&gt;The code you write is designed to solve a problem, and the tests you write are there to prove the code does what it says – the two are equally important, and should be treated as such.”&lt;/em&gt; - Paul&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At this point, it should be clear how critical it is to acknowledge tests as part of the codebase and treat them as such. In this regard, &lt;a href=&quot;https://twitter.com/martinho_t&quot;&gt;Tiago Martinho&lt;/a&gt;, the organiser of the &lt;a href=&quot;https://swiftpeerlab.github.io/&quot;&gt;Swift Peer Lab in Barcelona&lt;/a&gt;, goes one step further and explains how culture and adaptability work together to eliminate double standards:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;“Guidelines are important as a safety and communication mechanism, but the secret to maintaining a healthy codebase is having a healthy team culture. If we have guidelines for the production code but we are not following them for our tests, then there is a misalignment in the value these guidelines provide, and this should be tackled before adding more or changing existing guidelines.&lt;/em&gt;&lt;/p&gt;

  &lt;p&gt;&lt;em&gt;My personal opinion is that tests are part of the codebase, without them the cost of doing iterative development on the software greatly increases over time. On the other hand, if a test is not providing a safety net (that actually works) or is fragile with a high maintenance cost, then the best approach is to gradually migrate it to follow the team principles.”&lt;/em&gt; - Tiago&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The following sections contain some examples in how you can improve your tests by giving them as much attention as you give to your application’s code.&lt;/p&gt;

&lt;h2 id=&quot;call-it-magic&quot;&gt;Call It Magic&lt;/h2&gt;

&lt;p&gt;A magic number (or any other type) is a hard-coded value which meaning is hard to infer without context. Also, it might need to be updated at a later stage, therefore requiring you to change it in multiple places if it is all over your codebase.&lt;/p&gt;

&lt;p&gt;Everyone tends to avoid it in their projects, but it isn’t uncommon to see it in tests.&lt;/p&gt;

&lt;p&gt;Take a look at the following example:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/e65d46b7959b29ac0a09fd7ef56d5d52.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;While you might quickly notice the number 7, in order to understand it, you have to take a few seconds analyzing both functions. You might end realizing there is a business rule limiting the number of products an unauthenticated user will see. However, if instead of a magic number, you defined a constant with a proper name, both readability and understandability would have been improved.&lt;/p&gt;

&lt;p&gt;Still not convinced if it is worth the effort? What if months later, your company decides to update from 6 items for non-logged users to 9? In the previous example, you would &lt;em&gt;only&lt;/em&gt; have to update two tests but what if you had more? That’s right, using a magic number isn’t scalable at all.&lt;/p&gt;

&lt;p&gt;So instead, you could do as follows:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/2832fba065df686155401ac8ec22e172.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Now, if there is an update, you just change one value in your tests. Besides, you can immediately see the reasons behind what you are asserting.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/ZeMiguelFig&quot;&gt;José Figueiredo&lt;/a&gt;, a senior engineer at &lt;a href=&quot;https://synchronoss.com&quot;&gt;Synchronoss Technologies&lt;/a&gt;, also disagrees with the use of magic numbers and double standards in general. According to him:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;“In my honest opinion, magic values have no place in the middle of the code either if its production-grade or on a test target. Instead, they should be contextualised with variables, struct or enums with proper naming.&lt;/em&gt;&lt;/p&gt;

  &lt;p&gt;&lt;em&gt;After all, if you avoid the use of magic numbers in production code, you should be compelled to do it on the test target as well.”&lt;/em&gt; - José&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;hide-yo-data&quot;&gt;Hide yo’ Data&lt;/h2&gt;

&lt;p&gt;It is uncommon to have data stubs in the application’s code. However, it is very likely to have them in its tests. While data stubs aren’t an issue by themselves, spreading them across your test case isn’t a proper solution. Not only do they pollute it with extra lines but do also reduce its readability.&lt;/p&gt;

&lt;p&gt;Take the following as an example:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/9cd12e99e90db69057893c7c7ee047ef.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;In two tests, a considerable amount of lines are for instantiating two &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Product&lt;/code&gt; and for what? In the end, you care about what these objects represent, not how they are instantiated. So instead, place them strategically into isolated containers, expose their meaning and reuse them if appropriate. Not only will this reduce the lines of code in your test file but also increase its readability - not to mention you now have two &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Product&lt;/code&gt; you can reuse across your tests.&lt;/p&gt;

&lt;p&gt;As an example, create the following in a separate file:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/b76e3fe4dc50e3f8080e1711c00f92ff.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Then, update your test as follows:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/08f17141a4e15289289e998a1a894ef9.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Much shorter, easier to read, easier to understand and easier to maintain.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/rlrg_6&quot;&gt;Rodrigo López-Romero&lt;/a&gt;, an engineer at &lt;a href=&quot;https://adidas.github.io&quot;&gt;Adidas&lt;/a&gt;, added you should also have a well-defined folder structure, just like you would have in your application’s code. According to him:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;“I find very useful to have a separate project folder consisting of all test data. It helps to make the testing code clean and neat. When it comes to stubbing network requests, I think it is useful to have the JSON responses in separate files, although you need to update those JSON files whenever the server side changes.”&lt;/em&gt; - Rodrigo&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Keep in mind you shouldn’t overuse your test data! Distribute it by logic, context or any other criteria and avoid crossing it. Logic receives updates over time and with so you may need to update your test data. If it is too spread all over your tests, it may undesirably set some of them on fire! 🔥&lt;/p&gt;

&lt;h2 id=&quot;copy-pastability&quot;&gt;Copy-Pastability&lt;/h2&gt;

&lt;p&gt;On what do we spend most of our time as programmers? Scalability, replaceability, understandability, reusability and other fancy terms. All because we strive for quality.&lt;/p&gt;

&lt;p&gt;If we have the same five lines of code in different places what do we do? Encapsulate it in a function. But what about tests?&lt;/p&gt;

&lt;p&gt;Imagine a scenario in which your screens’ structure is dynamically controlled by a back-end service and you want to validate that when receiving a new structure it updates accordingly. Take the following as an example:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/d8108a5519e9ee4fee1965d8cb0671f9.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Notice the pattern? The code responsible for loading a JSON file doesn’t add any value to these tests. After all, it isn’t relevant how it loads it. It adds unnecessary complexity and extra lines for you to read. Instead, try doing as follows:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/db355b8285b5b4a2467a4d2b097679c7.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;These tests exist to validate how the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;screenStructureManager&lt;/code&gt; determines and updates the screen structure. Therefore, you can encapsulate any supplementary logic in functions with a well-defined interface. Subsequently, you’ll reduce the lines of code in your test file and its readability. The DRY principle plays a big role both in your application’s code and its tests in order to avoid repeating yourself.&lt;/p&gt;

&lt;p&gt;Want to improve it even further? Decouple loading a JSON from this test case, make it as pure as possible and then you can freely reuse it across your tests’ codebase.&lt;/p&gt;

&lt;p&gt;In short, you can summarize this section with the following words from &lt;a href=&quot;https://twitter.com/twostraws&quot;&gt;Paul Hudson&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;“Take the time to clean up your tests so you isolate common setup, then add custom assertion functions to avoid repeating yourself. The end result will be shorter tests that are easier to read, easier to write, and easier to check when they fail.”&lt;/em&gt; - Paul&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;some-or-none&quot;&gt;Some or None?&lt;/h2&gt;

&lt;p&gt;Optionality as great as it is can sometimes be bothersome to tackle. However, why would you take care of it in your application’s code but ignore it in its tests?&lt;/p&gt;

&lt;p&gt;I could be writing about this, but instead, I will ask you to read John Sundell’s &lt;a href=&quot;https://www.swiftbysundell.com/posts/avoiding-force-unwrapping-in-swift-unit-tests&quot;&gt;article&lt;/a&gt; about this subject. My opinion is quite the same as his so I think we should apply DRY here! 😂&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Tests’ code quality must be as high as in your application as you’ll be spending hours creating, maintaining and debugging them, so make sure you create a healthy environment around it. Also, be as coherent as possible and use all the tools in your belt, from design patterns to principles. Don’t disregard your tests, they are an essential part of your codebase!&lt;/p&gt;

&lt;p&gt;For further reading, &lt;a href=&quot;https://twitter.com/twostraws&quot;&gt;Paul&lt;/a&gt; recommends &lt;a href=&quot;http://xunitpatterns.com/&quot;&gt;“XUnit Test Patterns: Refactoring Test Code”&lt;/a&gt; by Gerard Meszaros.&lt;/p&gt;

&lt;p&gt;Huge shoutout to &lt;a href=&quot;https://twitter.com/ZeMiguelFig&quot;&gt;José Figueiredo&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/twostraws&quot;&gt;Paul Hudson&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/rlrg_6&quot;&gt;Rodrigo López-Romero&lt;/a&gt; and &lt;a href=&quot;https://twitter.com/martinho_t&quot;&gt;Tiago Martinho&lt;/a&gt; for joining me. I would also like to praise &lt;a href=&quot;https://twitter.com/anafpf3&quot;&gt;Ana Filipa Ferreira&lt;/a&gt;, &lt;a href=&quot;https://www.linkedin.com/in/heitormpf&quot;&gt;Heitor Ferreira&lt;/a&gt; and &lt;a href=&quot;https://twitter.com/tiagomssilvaa&quot;&gt;Tiago Silva&lt;/a&gt; for their outstanding support and &lt;a href=&quot;https://twitter.com/NSMyself&quot;&gt;João Pereira&lt;/a&gt; for giving the “Let’s Talk About” original idea. ❤️&lt;/p&gt;

&lt;p&gt;Last but not least, if you have any subject you would like to see covered and/or discussed, let me know here or on &lt;a href=&quot;https://twitter.com/pedrommcarrasco&quot;&gt;Twitter&lt;/a&gt;! 👀&lt;/p&gt;

&lt;p&gt;Thanks for reading. ✨&lt;/p&gt;</content><author><name>Pedro Carrasco</name></author><category term="Let's Talk About" /><category term="Testing" /><summary type="html">Have you ever found yourself surrounded in a codebase where code guidelines seem to be ignored in its tests? It’s time to talk about this with José Figueiredo, Paul Hudson, Rodrigo López-Romero and Tiago Martinho.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://pedrommcarrasco.github.io/assets/posts/lets-talk-about-double-standards/banner.jpg" /><media:content medium="image" url="https://pedrommcarrasco.github.io/assets/posts/lets-talk-about-double-standards/banner.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Saving Your Screen</title><link href="https://pedrommcarrasco.github.io/posts/Saving-your-screen/" rel="alternate" type="text/html" title="Saving Your Screen" /><published>2018-12-04T00:00:00+00:00</published><updated>2018-12-04T00:00:00+00:00</updated><id>https://pedrommcarrasco.github.io/posts/Saving-your-screen</id><content type="html" xml:base="https://pedrommcarrasco.github.io/posts/Saving-your-screen/">&lt;p&gt;Building your own screen saver in Swift can be a tricky task since the template provided by Xcode wasn’t designed for it. Still, it’s far from being impossible.&lt;/p&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;According to &lt;a href=&quot;https://forums.developer.apple.com/people/eskimo&quot;&gt;&lt;em&gt;Quinn “The Eskimo!”&lt;/em&gt;&lt;/a&gt;, an Apple Staff Member on the &lt;a href=&quot;https://forums.developer.apple.com/&quot;&gt;Apple Developer Forums&lt;/a&gt;, it isn’t safe to create a screen saver in Swift since the current architecture uses an  &lt;a href=&quot;https://forums.developer.apple.com/message/268962#268962&quot;&gt;“NSBundle-style plugin”&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“The current screen saver architecture uses an NSBundle-style plug-in.  It’s not safe to create such plug-ins in Swift unless you control all the plug-ins and the app loading those plug-ins.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;“Note that this does not apply to modern plug-ins, which are all app extensions.  An app extension can be safely written in Swift because it runs in its own process.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Because of this, screen savers written in Swift need to be bundled with the required Swift standard libraries into the application package. While this may lead to maintainability issues that need to be addressed in every Swift release, it didn’t stop the community from building screen savers written in Swift. As an example, you have both &lt;a href=&quot;https://github.com/JohnCoates/Aerial&quot;&gt;Aerial&lt;/a&gt; &amp;amp; &lt;a href=&quot;https://github.com/pedrommcarrasco/Brooklyn&quot;&gt;Brooklyn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There’s also hope that the promised ABI stability that is coming with Swift 5 tackles some of these problems.&lt;/p&gt;

&lt;h2 id=&quot;whats-the-goal&quot;&gt;What’s the goal?&lt;/h2&gt;

&lt;p&gt;You’ll be aiming to develop a screen saver written exclusively in Swift. Design-wise, it’ll be simple since this article isn’t about creating a fancy animation but instead, give you the tools needed to develop and create your own screen saver.&lt;/p&gt;

&lt;p&gt;By the end of this article, you’ll achieve the following screen saver:&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://github.com/pedrommcarrasco/pedrommcarrasco.github.io/blob/master/assets/posts/saving-your-screen/goal.gif?raw=true&quot; alt=&quot;Goal&quot; /&gt;
&lt;/p&gt;

&lt;h2 id=&quot;setup&quot;&gt;Setup&lt;/h2&gt;

&lt;p&gt;Like any other application, to develop a screen saver, you’ll have to start by creating a project in Xcode.&lt;/p&gt;

&lt;p&gt;Xcode already has a screen saver template and you are going to use it to speed up this task. To do so, follow the next steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Select &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Create a new Xcode project&lt;/code&gt;;&lt;/li&gt;
  &lt;li&gt;Select the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;macOS&lt;/code&gt; tab;&lt;/li&gt;
  &lt;li&gt;Search for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Screen saver&lt;/code&gt; and select it;&lt;/li&gt;
  &lt;li&gt;Name it &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SavingYourScreen&lt;/code&gt; and choose its location.&lt;/li&gt;
&lt;/ol&gt;

&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;https://github.com/pedrommcarrasco/pedrommcarrasco.github.io/blob/master/assets/posts/saving-your-screen/CreateProject.gif?raw=true&quot; alt=&quot;Create Project&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;Following this, you’ll notice Xcode generated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SavingYourScreen.m&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SavingYourScreen.h&lt;/code&gt;. But since you are going to develop it in Swift you can safely delete these two files. Once deleted, create a Swift file named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SaverView&lt;/code&gt; and Xcode will present the following prompt:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/pedrommcarrasco/pedrommcarrasco.github.io/blob/master/assets/posts/saving-your-screen/BridgingHeader.png?raw=true&quot; alt=&quot;Bridging Header&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As you won’t work with Swift &amp;amp; Objective-C interoperability, just select &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Don't Create&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;coding-that-view&quot;&gt;Coding that View&lt;/h2&gt;

&lt;p&gt;To start things off, import the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ScreenSaver&lt;/code&gt; framework and subclass &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ScreenSaverView&lt;/code&gt; as follows:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/3dc7d6fae2f861f917ed0885c3c2b80a.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;With the skeleton ready, it is time to start working on the real implementation,  starting by implementing the component required to display an emoji.&lt;/p&gt;

&lt;p&gt;While in iOS you could use an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UILabel&lt;/code&gt; by importing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIKit&lt;/code&gt;, in macOS you don’t have access to it. Instead, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AppKit&lt;/code&gt; provides a component called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSTextField&lt;/code&gt;. Unfortunately, this component behaviour is very similar to its iOS counterpart, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UITextField&lt;/code&gt;. This means, it supports interaction &amp;amp; typing, which you don’t want and in order to remove these functionalities, you’re going to subclass &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSTextField&lt;/code&gt; as follows:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/3c98f5bc69965454012dd9ebe3ec0a9a.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Now, create an instance of it in your view by adding the following code above your initialization methods:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/f0104a17876a07c09369e5f3abb2b0d0.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Finally, implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;addSubviews&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;defineConstraints&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setupSubviews&lt;/code&gt; as follows:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/f60e6b250f72fef57db447d6f9df9474.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Everything should look very familiar to you and in fact, it is! Most of the code until now could be used in iOS except for some minor differences between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIKit&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AppKit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, you won’t be getting the result you are aiming for with it. While you’ve defined the layout for your view, you are still missing the random emojis changing every second.&lt;/p&gt;

&lt;p&gt;Start by creating a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt; extension with the following method:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/880c16ed6f8b763d705f7dc4d7856dc2.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;With it, you can easily retrieve an emoji but what about changing it every second? You could easily implement a timer but the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ScreenSaverView&lt;/code&gt; has some tools under its belt. According to Apple’s &lt;a href=&quot;https://developer.apple.com/documentation/screensaver/screensaverview&quot;&gt;documentation&lt;/a&gt;, it has the following methods:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;startAnimation()&lt;/code&gt; - &lt;em&gt;“Activates the periodic timer that animates the screen saver.”&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stopAnimation()&lt;/code&gt; - &lt;em&gt;“Deactivates the timer that advances the animation.”&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animateOneFrame()&lt;/code&gt; - &lt;em&gt;“Advances the screen saver’s animation by a single frame.”&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These three methods together provide you a timer embed into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ScreenSaverView&lt;/code&gt;’s life cycle. Both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;startAnimation()&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stopAnimation&lt;/code&gt; are called automatically. To control the pace in which &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animateOneFrame()&lt;/code&gt; is triggered, you need to set the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animationTimeInterval&lt;/code&gt; property. Since your goal is to show one emoji per second, you can safely define it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; inside your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;configure&lt;/code&gt; method. In short, this means that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animateOneFrame()&lt;/code&gt; will be called every second.&lt;/p&gt;

&lt;p&gt;In order to use it, start by adding the following method to your view. It will be responsible for updating the emoji presented on your screen:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/c5f39e4f7609e1e54d4963f05d4b1271.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Now, you just have to override &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;animateOneFrame()&lt;/code&gt; and call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;updateContent()&lt;/code&gt; after &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;super.animateOneFrame()&lt;/code&gt;. In the end, your view should look as follows:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/0b31150ba8415f73aee6c021188c84c1.js&quot;&gt;&lt;/script&gt;

&lt;h2 id=&quot;time-to-get-your-hands-dirty&quot;&gt;Time to get your hands dirty&lt;/h2&gt;

&lt;p&gt;If you attempt to install it by building and opening the generated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.saver&lt;/code&gt; (right-click &amp;amp; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Open with External Editor&lt;/code&gt; as shown in the image below), you’ll notice that instead of seeing your screen saver you’ll face &lt;em&gt;“You cannot use the SavingYourScreen screen saver with this version of macOS. Please contact the vendor to get a newer version of the screen saver.”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/pedrommcarrasco/pedrommcarrasco.github.io/blob/master/assets/posts/saving-your-screen/Install.png?raw=true&quot; alt=&quot;Bridging Header&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As mentioned earlier in this article, screen savers were meant to be written in Objective-C and its template is prepared according to it.&lt;/p&gt;

&lt;p&gt;To solve this, start by going to your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Info.plist&lt;/code&gt; and replace the value for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Principal Class&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SavingYourScreen.SaverView&lt;/code&gt; which corresponds to your target and view joined by a dot.&lt;/p&gt;

&lt;p&gt;Now, go to the build settings of your project and change the value for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Always Embed Swift Standard Libraries&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YES&lt;/code&gt;. This will ensure that any of the required Swift standard libraries are also copied into your app bundle.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Although it isn’t recommended to create a screen saver in Swift, it isn’t difficult to go around it, you just need to keep maintaining it every time a Swift version gets released. ABI stability is promised to come with Swift 5 but in the meantime, you have all the tools needed to create your own screen saver.&lt;/p&gt;

&lt;p&gt;Take into consideration that you have only scratched the surface when it comes to creativity &amp;amp; possibilities. This article introduces you to screen savers in Swift but you can go further and beyond by downloading resources from any service, draw custom shapes and the list goes on.&lt;/p&gt;

&lt;p&gt;In the end, what’s your opinion of it? Have you ever tried to build a screen saver or was it something new to you? Let me know, alongside with your questions, by sending feedback on &lt;a href=&quot;https://twitter.com/pedrommcarrasco&quot;&gt;Twitter&lt;/a&gt; or here.&lt;/p&gt;

&lt;p&gt;Last but not least, I would like to praise &lt;a href=&quot;https://twitter.com/anafpf3&quot;&gt;Ana Filipa Ferreira&lt;/a&gt;, &lt;a href=&quot;https://linkedin.com/in/heitormpf&quot;&gt;Heitor Ferreira&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/nsmyself&quot;&gt;João Pereira&lt;/a&gt; and &lt;a href=&quot;https://twitter.com/&quot;&gt;José Figueiredo&lt;/a&gt; for their outstanding support. ❤️&lt;/p&gt;

&lt;p&gt;Thanks for reading. ✨&lt;/p&gt;</content><author><name>Pedro Carrasco</name></author><category term="Guide" /><category term="macOS" /><summary type="html">Building your own screen saver in Swift can be a tricky task since the template provided by Xcode wasn’t designed for it. Still, it’s far from being impossible.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://pedrommcarrasco.github.io/assets/posts/saving-your-screen/banner.jpg" /><media:content medium="image" url="https://pedrommcarrasco.github.io/assets/posts/saving-your-screen/banner.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Power-Up Your Anchors</title><link href="https://pedrommcarrasco.github.io/posts/power-up-your-anchors/" rel="alternate" type="text/html" title="Power-Up Your Anchors" /><published>2018-10-25T00:00:00+00:00</published><updated>2018-10-25T00:00:00+00:00</updated><id>https://pedrommcarrasco.github.io/posts/power-up-your-anchors</id><content type="html" xml:base="https://pedrommcarrasco.github.io/posts/power-up-your-anchors/">&lt;p&gt;Programmatically done Auto Layout is still the preferred way of implementing views by a lot of developers. While there are amazing open-source frameworks, most of them differ from Apple’s anchor syntax. Therefore, by adding them to your project, you’ll raise the entry level complexity of your project and increase its learning curve. In this article, you’ll learn how to avoid adding an external dependency and create your own layer above &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor&lt;/code&gt; to solve some of its issues.&lt;/p&gt;

&lt;h3 id=&quot;introduction&quot;&gt;Introduction&lt;/h3&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor&lt;/code&gt; was first introduced by Apple in iOS 9.0 and it is described as a &lt;em&gt;“factory class for creating layout constraint objects using a fluent API”&lt;/em&gt;.  Apple’s &lt;a href=&quot;https://developer.apple.com/documentation/uikit/nslayoutanchor&quot;&gt;documentation&lt;/a&gt;  also refers that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor&lt;/code&gt; usage is preferred when compared to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutConstraint&lt;/code&gt;: “use these constraints to programmatically define your layout using Auto Layout. Instead of creating &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutConstraint&lt;/code&gt; …”. This is due to type checking and having a cleaner interface when compared to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutConstraint&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Improvements related to type checking are based in Apple’s decision to split &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor&lt;/code&gt; into three different concepts, being:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutXAxisAnchor&lt;/code&gt; for horizontal constraints&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutYAxisAnchor&lt;/code&gt; for vertical constraints&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutDimension&lt;/code&gt; for width and height constraints&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Apple’s &lt;a href=&quot;https://developer.apple.com/documentation/uikit/nslayoutanchor&quot;&gt;documentation&lt;/a&gt; also mentions that “&lt;em&gt;you never use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor&lt;/code&gt; class directly. Instead, use one of its subclasses, based on the type of constraint you wish to create&lt;/em&gt;”. In short, you can never constrain anchors between the different subclasses shown above. But, you can still mess it up, as Apple states:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;“While the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor&lt;/code&gt; class provides additional type checking, it is still possible to create invalid constraints, For example, the compiler allows you to constrain one view’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leadingAnchor&lt;/code&gt; with another view’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;leftAnchor&lt;/code&gt;, since they are both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutXAxisAnchor&lt;/code&gt; instances. However, Auto Layout does not allow constraints that mix leading and trailing attributes with left or right attributes”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First of all, take a look at the following code and try to identify some of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor&lt;/code&gt;’s boilerplate code.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/c6957ba7fcd8519d82d73f9d1b8275c7.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;According to this implementation, you should have found the following requirements:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;You must set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;translatesAutoresizingMaskIntoConstraints&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt; for every view;&lt;/li&gt;
  &lt;li&gt;You must activate constraints by setting its property &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isActive&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; or by using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutConstraint.activate()&lt;/code&gt;;&lt;/li&gt;
  &lt;li&gt;You cannot set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UILayoutPriority&lt;/code&gt; via a parameter and must instead create a variable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Within this article, you will learn how to address these issues. However, keep in mind that this only applies to Swift.&lt;/p&gt;

&lt;h3 id=&quot;translatesautor-yes-that-long-property-you-always-set-to-false&quot;&gt;TranslatesAutor… Yes, that long property you always set to false&lt;/h3&gt;

&lt;p&gt;Here lies the first identified issue, and Apple is clear on why you always have to set it as false:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“&lt;em&gt;If this property’s value is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;, the system creates a set of constraints that duplicate the behavior specified by the view’s autoresizing mask. This also lets you modify the view’s size and location using the view’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;frame&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bounds&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;center&lt;/code&gt; properties&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;em&gt;If you want to use Auto Layout to dynamically calculate the size and position of your view, you must set this property to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;&lt;/em&gt;”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This describes exactly what you want: use Auto Layout to dynamically calculate the size and position of your views. However you don’t want to write this huge property for every single view, not even inside a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forEach&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There are multiple approaches to solve this, but for now you will be improving &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;addSubview&lt;/code&gt; and adding a side-effect to it. To do so, create an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIView&lt;/code&gt; extension with the following code:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/be50ad4da9a743c791ee07fbd39ee970.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;With this code, you will be able to send multiple views, set them all as subviews and set each one’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;translatesAutoresizingMaskIntoConstraints&lt;/code&gt; to false all at once.&lt;/p&gt;

&lt;p&gt;Now, instead of doing the following:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/ea6052781799fcefc3ed2f2ca8bceea4.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;You will now have:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/fe03e548c9b916b11278188bc192a2e5.js&quot;&gt;&lt;/script&gt;

&lt;h3 id=&quot;what-about-the-remaining-issues&quot;&gt;What about the remaining issues?&lt;/h3&gt;

&lt;p&gt;Start by extending &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor&lt;/code&gt; as follows:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/190b5a091917d1307c26f0ef0b64e85c.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;This will generate an error:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/pedrommcarrasco/pedrommcarrasco.github.io/blob/master/assets/posts/power-up-your-anchors/objcerror.png?raw=true&quot; alt=&quot;ObjcGenericError&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Prior to Swift 4, you were forced to extend each of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor’&lt;/code&gt;s subclasses, or constrain it, because it is a generic class. But now you can simply expose your extension to Objective-C.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/9655a5c2ccd468ad6ea6ae98e01d046b.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;If you try to compile this, you will notice that the error disappears. Your extension is now ready to use your own code, and that is exactly what you’re going to do.&lt;/p&gt;

&lt;h3 id=&quot;activate-your-constraints&quot;&gt;Activate your constraints!&lt;/h3&gt;

&lt;p&gt;Setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isActive&lt;/code&gt; in every single anchor is excruciating. Even though &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutConstraint.activate()&lt;/code&gt; might be considered a better option, according to Apple’s &lt;a href=&quot;https://developer.apple.com/documentation/uikit/nslayoutconstraint/1526955-activate&quot;&gt;documentation&lt;/a&gt;, it still adds a lot of indentation.&lt;/p&gt;

&lt;p&gt;One way of solving this would be to set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isActive&lt;/code&gt; to true by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;default&lt;/code&gt;. You can achieve this with the following:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/394200fedad867b63f504f149301afe4.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;This function uses a Swift capability called default argument. It allows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isActive&lt;/code&gt; to be called as an optional argument. By default, it will always be set to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;. But in case you don’t want it active, you can set it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;By using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@discardableResult&lt;/code&gt;, you will be returning an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutConstraint&lt;/code&gt; that you can safely ignore if you don’t need it. In case you never heard about this keyword, I’ve written an article titled “&lt;a href=&quot;https://pedrommcarrasco.github.io/posts/i-want-to-be-discardable/&quot;&gt;I Want to be Discardable&lt;/a&gt;” that addresses this.&lt;/p&gt;

&lt;h3 id=&quot;too-many-functions&quot;&gt;Too many functions!&lt;/h3&gt;

&lt;p&gt;Currently, you only support a relation of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equalTo&lt;/code&gt; when you should also support &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;greaterThanOrEqualTo&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lessThanOrEqualTo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;According to Apple’s &lt;a href=&quot;https://developer.apple.com/documentation/uikit/nslayoutanchor&quot;&gt;documentation&lt;/a&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor&lt;/code&gt; exposes 6 different functions. These are as follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;func constraint(equalTo: NSLayoutAnchor) -&amp;gt; NSLayoutConstraint&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;func constraint(equalTo: NSLayoutAnchor, constant: CGFloat) -&amp;gt; NSLayoutConstraint&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;func constraint(greaterThanOrEqualTo: NSLayoutAnchor) -&amp;gt; NSLayoutConstraint&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;func constraint(greaterThanOrEqualTo: NSLayoutAnchor, constant: CGFloat) -&amp;gt; NSLayoutConstraint&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;func constraint(lessThanOrEqualTo: NSLayoutAnchor) -&amp;gt; NSLayoutConstraint&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;func constraint(lessThanOrEqualTo: NSLayoutAnchor, constant: CGFloat) -&amp;gt; NSLayoutConstraint&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While Apple’s approach works, you can reduce the amount of functions in your interface with an enumeration approach for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutConstraint.Relation&lt;/code&gt; as in the following code:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/0d5d088b47c627b9509183503c745b51.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;If you try to use it:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/04d59262eea41b9ddc4430d029eedcab.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Everything seems to be working well. But what if you try to apply a width constraint based on a constant? Add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b.widthAnchor.constrain(to: 50.0)&lt;/code&gt; to the previous example and rebuild. Oh no, you trigger another error:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/pedrommcarrasco/pedrommcarrasco.github.io/blob/master/assets/posts/power-up-your-anchors/missingfunction.png?raw=true&quot; alt=&quot;MissingFunction&quot; /&gt;&lt;/p&gt;

&lt;p&gt;But this time it is pretty easy to understand what’s happening. Currently, your function is expecting you to send a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To solve this, the following are the two most obvious solutions:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Set the expecting anchor as an optional, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor?&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Provide a default parameter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But none of these are optimal, because:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;It would lead to some edge cases that aren’t supposed to be possible and wouldn’t even work. Therefore your interface would allow inconsistencies that didn’t exist before.&lt;/li&gt;
  &lt;li&gt;It isn’t possible to provide a valid default parameter to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;According to Apple’s documentation, you are only allowed to set a constraint without any relation to another anchor for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutDimension&lt;/code&gt;’s anchors.&lt;/p&gt;

&lt;p&gt;To avoid missing cases that already exist, you should also have the option to apply a constraint between two &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayouDimension&lt;/code&gt; anchors with a constant &amp;amp; multiplier.&lt;/p&gt;

&lt;p&gt;In order to address this, you must enable this feature only for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutDimension&lt;/code&gt;’s anchors. Therefore, you are going to extend &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutDimension&lt;/code&gt; with the following code:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/550c1a0828d3245079382d318b298906.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;You’ll now able to apply width and height constraints without any kind of relation to another anchor.&lt;/p&gt;

&lt;h3 id=&quot;priorities&quot;&gt;Priorities&lt;/h3&gt;

&lt;p&gt;Now, you’re going to simplify setting priorities. To set a constraint’s priority in the current model, with anchors, you would need to do the following:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/1819ab318af946b29b0e5998b2d666a1.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;A good way to avoid being forced to assign the constraint to a property is adding this option to your functions. Also, with a default value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.required&lt;/code&gt;, you don’t need to type it in most scenarios.&lt;/p&gt;

&lt;p&gt;Replace your functions with the following:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/b4dbb92ff435df63dfc5f8861ea3bff8.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;However, what if you wanted a priority between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.defaultHigh&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.required&lt;/code&gt;? The current way doesn’t look very clean. In order to improve it, you are going to extend &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutPriority&lt;/code&gt; with the following:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/866dbdf718c28e7a94bad4fab68c3810.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;And now you’ll be able to do &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bWidth.priority = .defaultHigh + 1&lt;/code&gt; .&lt;/p&gt;

&lt;h3 id=&quot;dry&quot;&gt;DRY&lt;/h3&gt;

&lt;p&gt;DRY stands for “&lt;a href=&quot;https://en.wikipedia.org/wiki/Don%27t_repeat_yourself&quot;&gt;Don’t Repeat Yourself&lt;/a&gt;”, and it is often seen as a golden rule in software development.&lt;/p&gt;

&lt;p&gt;You are currently repeating yourself when setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isActive&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;priority&lt;/code&gt;. In order to follow this pattern, you will move this to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutConstraint&lt;/code&gt; extension.&lt;/p&gt;

&lt;p&gt;Start by adding the following function to your extension:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/222e09cc794f51c203e7262e654939c6.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;And now update your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constrain&lt;/code&gt; functions with the following:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/0866f2260656c8690b5e4ec02a5261de.js&quot;&gt;&lt;/script&gt;

&lt;h3 id=&quot;what-have-you-done&quot;&gt;What have you done?&lt;/h3&gt;

&lt;p&gt;At the beginning of this article, you spotted some problems related to anchors. Now you can proudly check that you’ve developed solutions to each of them.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Having to set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;translatesAutoresizingMaskIntoConstraints&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt; for every view that isn’t loaded from a NIB
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;You’ve created a new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;addSubviewsUsingAutoLayout&lt;/code&gt; function that supports sending multiple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIView&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Activating constraints by setting its property &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isActive&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;, or using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutConstraint.activate()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UILayoutPriority&lt;/code&gt; via parameter is not supported and requires you to create a variable.
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor&lt;/code&gt; extensions work together to solve these two problems&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Interoperability has its costs to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor&lt;/code&gt; because it doesn’t allow it to take advantage of some Swift capabilities.
    &lt;ul&gt;
      &lt;li&gt;&lt;strong&gt;You’ve reduced the number of functions needed with an enum based approach and adopted default parameters in your interface.&lt;/strong&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything is looking good on paper. However, how does it look in practice?&lt;/p&gt;

&lt;h3 id=&quot;how-does-it-look&quot;&gt;How does it look?&lt;/h3&gt;

&lt;p&gt;Before showcasing your new and fresh powered-up anchors, take another look at the initial example shown in this article:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/c6957ba7fcd8519d82d73f9d1b8275c7.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Now you can achieve the same result in a much cleaner way with your extensions:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/3329b41081d8798c837852e17d87a78e.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;As you can see, it is a lot easier to read and understand what each anchor is doing. You were also able to decrease the amount of code you needed to write.&lt;/p&gt;

&lt;h3 id=&quot;future-improvements&quot;&gt;Future Improvements&lt;/h3&gt;

&lt;p&gt;While this will simplify the usage of Auto Layout programmatically, there are some details missing. The following functions aren’t covered:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constraintEqualToSystemSpacingBelow(NSLayoutYAxisAnchor, multiplier: CGFloat) &lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constraintGreaterThanOrEqualToSystemSpacingBelow(NSLayoutYAxisAnchor, multiplier: CGFloat) &lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constraintLessThanOrEqualToSystemSpacingBelow(NSLayoutYAxisAnchor, multiplier: CGFloat) &lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constraintEqualToSystemSpacingAfter(NSLayoutXAxisAnchor, multiplier: CGFloat)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constraintGreaterThanOrEqualToSystemSpacingAfter(NSLayoutXAxisAnchor, multiplier: CGFloat)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;constraintLessThanOrEqualToSystemSpacingAfter(NSLayoutXAxisAnchor, multiplier: CGFloat)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If requested, we can review this in a follow-up article.&lt;/p&gt;

&lt;h3 id=&quot;the-end&quot;&gt;The End&lt;/h3&gt;

&lt;p&gt;If you’ve managed to fully read this and reach the end, congratulations on powering your anchors!
In the end, what’s your opinion of it? Did you know it already, or was it something new to you? Let me know, alongside with your questions, by sending feedback on &lt;a href=&quot;https://twitter.com/pedrommcarrasco&quot;&gt;Twitter&lt;/a&gt; or here.&lt;/p&gt;

&lt;p&gt;Last but not least, I would like to praise &lt;a href=&quot;https://twitter.com/anafpf3&quot;&gt;Ana Filipa Ferreira&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/NSMyself&quot;&gt;João Pereira&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/ZeMiguelFig&quot;&gt;José Figueiredo&lt;/a&gt;, &lt;a href=&quot;https://www.linkedin.com/in/peusebio/&quot;&gt;Pedro Eusébio&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/tiagomssilvaa&quot;&gt;Tiago Silva&lt;/a&gt; and &lt;a href=&quot;https://twitter.com/farfetch&quot;&gt;Farfetch&lt;/a&gt; for their outstanding support. ❤️&lt;/p&gt;

&lt;p&gt;Thanks for reading. ✨&lt;/p&gt;</content><author><name>Pedro Carrasco</name></author><category term="Guide" /><category term="Auto Layout" /><summary type="html">Programmatically done Auto Layout is still the preferred way of implementing views by a lot of developers. While there are amazing open-source frameworks, most of them differ from Apple’s anchor syntax. Therefore, by adding them to your project, you’ll raise the entry level complexity of your project and increase its learning curve. In this article, you’ll learn how to avoid adding an external dependency and create your own layer above NSLayoutAnchor to solve some of its issues.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://pedrommcarrasco.github.io/assets/posts/power-up-your-anchors/banner.jpg" /><media:content medium="image" url="https://pedrommcarrasco.github.io/assets/posts/power-up-your-anchors/banner.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Guide Your Layout</title><link href="https://pedrommcarrasco.github.io/posts/guide-your-layout/" rel="alternate" type="text/html" title="Guide Your Layout" /><published>2018-08-24T00:00:00+00:00</published><updated>2018-08-24T00:00:00+00:00</updated><id>https://pedrommcarrasco.github.io/posts/guide-your-layout</id><content type="html" xml:base="https://pedrommcarrasco.github.io/posts/guide-your-layout/">&lt;p&gt;How would you achieve the following screen?&lt;/p&gt;

&lt;p align=&quot;center&quot;&gt;
&lt;img src=&quot;https://github.com/pedrommcarrasco/pedrommcarrasco.github.io/blob/master/assets/posts/guide-your-layout/goal.jpg?raw=true&quot; alt=&quot;Goal&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;You can easily spot an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIImageView&lt;/code&gt; and an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UILabel&lt;/code&gt; centered together.&lt;/p&gt;

&lt;p&gt;There are multiple approaches to this problem and the most obvious would be to insert these two elements in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIStackView&lt;/code&gt;. But what if, in a more complex layout, you weren´t able to use it? The most common approach, although not the best, would be encapsulating the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIImageView&lt;/code&gt; and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UILabel&lt;/code&gt; inside a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIView&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this article, you’ll learn how to approach this problem in this scenario.&lt;/p&gt;

&lt;h2 id=&quot;im-here-to-guide-you&quot;&gt;I’m Here to Guide You!&lt;/h2&gt;

&lt;p&gt;In iOS 9.0, Apple introduced major changes to its frameworks and added new ones. One of them was &lt;a href=&quot;https://developer.apple.com/documentation/uikit/uilayoutguide&quot;&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UILayoutGuide&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UILayoutGuide&lt;/code&gt; is a &lt;em&gt;“rectangular area that can interact with Auto Layout”&lt;/em&gt;. It should be used to replace any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIView&lt;/code&gt; used for &lt;em&gt;“inter-view spaces or encapsulation”&lt;/em&gt; since it improved efficiency and safety by avoiding the following issues regarding these kind of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIView&lt;/code&gt; (dummy views):&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Creating and maintaining a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIView&lt;/code&gt; impacts performance&lt;/li&gt;
  &lt;li&gt;Each member of the view hierarchy adds overhead to every task the hierarchy performs&lt;/li&gt;
  &lt;li&gt;A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dummyView&lt;/code&gt; will be able to intercept messages that are intended for other views&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It’s also important to notice that, similar to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIStackView&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UILayoutGuide&lt;/code&gt; cannot have a background color, among other properties.&lt;/p&gt;

&lt;h2 id=&quot;worlds-most-famous-guide&quot;&gt;World’s Most Famous Guide&lt;/h2&gt;

&lt;p&gt;Alongside with iOS 11, Apple introduced the Safe Area Layout Guide while deprecating the top and bottom layout guides.&lt;/p&gt;

&lt;p&gt;According to its &lt;a href=&quot;https://developer.apple.com/documentation/uikit/uiview/2891102-safearealayoutguide&quot;&gt;documentation&lt;/a&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;safeAreaLayoutGuide&lt;/code&gt; is &lt;em&gt;“the layout guide representing the portion of your view that is unobscured by bars and other content”&lt;/em&gt;. It can also be confirmed in its declaration below, that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;safeAreaLayoutGuide&lt;/code&gt; truly is an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UILayoutGuide&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/pedrommcarrasco/pedrommcarrasco.github.io/blob/master/assets/posts/guide-your-layout/safeAreaDeclaration.png?raw=true&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;time-to-solve-problems&quot;&gt;Time to Solve Problems&lt;/h2&gt;

&lt;p&gt;With all the knowledge acquired, you are now able to solve the initial problem using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UILayoutGuide&lt;/code&gt; instead of a dummy view.&lt;/p&gt;

&lt;p&gt;To start off, if you were using an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIView&lt;/code&gt; for encapsulation, you would start by creating it and adding it to the hierarchy:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/fedb314239e484fdc2e7f9429a3786c8.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;This time, since you’re using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UILayoutGuide&lt;/code&gt;, you’ll need to create an instance of it. Then, instead of adding it by calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;addSubview(UIView)&lt;/code&gt;, you’ll be using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;addLayoutGuide(UILayoutGuide)&lt;/code&gt;.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/a62604dbb5b4947842ca558a9264817c.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Notice that, since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UILayoutGuide&lt;/code&gt; isn’t a member of the view hierarchy you’ll be adding your views to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;view&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;layoutGuide&lt;/code&gt;.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/5a56b64899e6a9dbe1f61c312ca21b97.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;You’re now done with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UILayoutGuide&lt;/code&gt;’s initial setup and with so, it’s time to move to constraints. In the end, it is similar to what you would do with an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIView&lt;/code&gt;.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/11ad48d9a90e66d5586c01b43b14f261.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Keep in mind that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;photoImageView&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;introLabel&lt;/code&gt; setup isn’t in the scope of this article. Still, if you’re interested in it, take a look &lt;a href=&quot;https://github.com/pedrommcarrasco/pedrommcarrasco.github.io/tree/master/Articles-Source-Code/Guide%20your%20layout/GuideYourLayout.playground&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Most of the times, you’ll end up using a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UIStackView&lt;/code&gt; to solve problems alike but there are cases where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UILayoutGuide&lt;/code&gt; shines. The presented problem isn’t the case and it was just used for a matter of presentation.&lt;/p&gt;

&lt;p&gt;Unfortunately, in case you prefer working with Interface Builder, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UILayoutGuide&lt;/code&gt; isn’t available there.&lt;/p&gt;

&lt;p&gt;In the end, what’s your opinion of it? Did you know it or is it something new to you? Let me know it, alongside with your questions and feedback on &lt;a href=&quot;https://twitter.com/pedrommcarrasco&quot;&gt;Twitter&lt;/a&gt; or here.&lt;/p&gt;

&lt;p&gt;Last but not least, I would like to praise &lt;a href=&quot;https://twitter.com/anafpf3&quot;&gt;Ana Filipa Ferreira&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/tiagomssilvaa&quot;&gt;Tiago Silva&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/ZeMiguelFig&quot;&gt;José Figueiredo&lt;/a&gt; and &lt;a href=&quot;https://twitter.com/NSMyself&quot;&gt;João Pereira&lt;/a&gt; for their outstanding support. ❤️&lt;/p&gt;

&lt;p&gt;Thanks for reading. ✨&lt;/p&gt;</content><author><name>Pedro Carrasco</name></author><category term="Tips &amp; Tricks" /><category term="Auto Layout" /><summary type="html">How would you achieve the following screen?</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://pedrommcarrasco.github.io/assets/posts/guide-your-layout/banner.jpg" /><media:content medium="image" url="https://pedrommcarrasco.github.io/assets/posts/guide-your-layout/banner.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">I Want To Be Discardable</title><link href="https://pedrommcarrasco.github.io/posts/i-want-to-be-discardable/" rel="alternate" type="text/html" title="I Want To Be Discardable" /><published>2018-07-19T00:00:00+00:00</published><updated>2018-07-19T00:00:00+00:00</updated><id>https://pedrommcarrasco.github.io/posts/i-want-to-be-discardable</id><content type="html" xml:base="https://pedrommcarrasco.github.io/posts/i-want-to-be-discardable/">&lt;p&gt;Ever wanted to ignore a value returned from a function but if you do so you’ll end up with the following warning?&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/pedrommcarrasco/pedrommcarrasco.github.io/blob/master/assets/posts/i-want-to-be-discardable/warning.png?raw=true&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;First of all, take the following function as an example for this article.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/1953479473615c270c86bed56d5185ad.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;In most scenarios, you would use ‘&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_ = example()&lt;/code&gt;’ to silence the warning. While this is correct, adding ‘&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_ =&lt;/code&gt;’ every single time would create a lot of boilerplate code and your public interface wouldn’t seem well designed. Keep in mind that changing the function to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Void&lt;/code&gt; isn’t an option for this specific example because we might want to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;example()&lt;/code&gt;’s return in some scenario.&lt;/p&gt;

&lt;h2 id=&quot;the-hero&quot;&gt;The Hero&lt;/h2&gt;

&lt;p&gt;Prior to Swift 2, this warning wouldn’t appear unless you’ve marked your function with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@warn_unused_result&lt;/code&gt;. In Swift 3, Apple turned on this warning by default and introduced &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@discardableResult&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;“&lt;em&gt;Apply this attribute to a function or method declaration to suppress the compiler warning when the function or method that returns a value is called without using its result.&lt;/em&gt;” - &lt;a href=&quot;https://docs.swift.org/swift-book/ReferenceManual/Attributes.html&quot;&gt;Swift.org&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This mark is available since iOS 8.0.&lt;/p&gt;

&lt;h2 id=&quot;ignore-this-ignore-that-i-want-those&quot;&gt;Ignore this, Ignore that, I want those&lt;/h2&gt;

&lt;p&gt;Previously you’ve used ‘&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_ =&lt;/code&gt;’ to ignore a function result, but this time, you are going to do that by using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@discardableResult&lt;/code&gt;. To do so, you’ll add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@discardableResult&lt;/code&gt; to the previous function. Resulting in:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/3314a3485b42ad2ed1573cf322866876.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;Now, invoke your function, compile and you’ll verify that Xcode didn’t show any kind of warning this time. But what if you want to use the value returned?&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/cb2d294460eadd51131ff5138c58932e.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;If you run the code above you’ll see that “This is an example” is printed followed by “This is an example with discardable”, meaning that you’ve successfully stored the result of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;example()&lt;/code&gt; in  &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;value&lt;/code&gt;, appended “ with discardable to it” and finally printed it.&lt;/p&gt;

&lt;h2 id=&quot;real-world-examples&quot;&gt;Real-World Examples&lt;/h2&gt;

&lt;p&gt;You don’t have to look too far to find some real-world examples. Take a look at Apple’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutAnchor&lt;/code&gt; for example. If you notice when you apply a constraint with anchors, in case you need to save it locally to define a priority, you could easily do:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/pedrommcarrasco/abc6a3baf8c007bd207e5ed9f447334c.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;If you don’t want use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NSLayoutConstraint&lt;/code&gt; returned, you’re also allowed to ignore it without Xcode displaying any warning.&lt;/p&gt;

&lt;p&gt;There are many other cases where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@discardableResult&lt;/code&gt; can be useful, for example, it can also be handy for chaining functions and logging.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;So, should you use it every single time you have a return value?
The answer is no, absolutely not. Your interfaces should be as clear as possible and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@discardableResult&lt;/code&gt; usually means your function has side-effects and adds a small implementation detail that doesn’t have much visibility, therefore, it could lead to some readability problems and inconsistencies. Plus, it shouldn’t be used when you are expecting to use the result of a function most of the times.&lt;/p&gt;

&lt;p&gt;And with this, you’ve now acknowledged &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@discardableResult&lt;/code&gt;. What’s your opinion about it? Did you know it or is it something new to you? Let me hear it, alongside with your questions and feedback on &lt;a href=&quot;https://twitter.com/pedrommcarrasco&quot;&gt;Twitter&lt;/a&gt; or here.&lt;/p&gt;

&lt;p&gt;Last but not least, I would like to praise &lt;a href=&quot;https://twitter.com/anafpf3&quot;&gt;Ana Filipa Ferreira&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/tiagomssilvaa&quot;&gt;Tiago Silva&lt;/a&gt;, &lt;a href=&quot;https://www.linkedin.com/in/peusebio/&quot;&gt;Pedro Eusébio&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/ZeMiguelFig&quot;&gt;José Figueiredo&lt;/a&gt;, &lt;a href=&quot;https://twitter.com/TroutDev&quot;&gt;Warren Burton&lt;/a&gt; and &lt;a href=&quot;https://twitter.com/NSMyself&quot;&gt;João Pereira&lt;/a&gt; for their outstanding support. ❤️&lt;/p&gt;

&lt;p&gt;Thanks for reading. ✨&lt;/p&gt;</content><author><name>Pedro Carrasco</name></author><category term="Tips &amp; Tricks" /><category term="Language Feature" /><summary type="html">Ever wanted to ignore a value returned from a function but if you do so you’ll end up with the following warning?</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://pedrommcarrasco.github.io/assets/posts/i-want-to-be-discardable/banner.jpg" /><media:content medium="image" url="https://pedrommcarrasco.github.io/assets/posts/i-want-to-be-discardable/banner.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry></feed>