<?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 Luis Mesquita on Medium]]></title>
        <description><![CDATA[Stories by Luis Mesquita on Medium]]></description>
        <link>https://medium.com/@luiscmesquita?source=rss-889f88d3cfd7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*PBQu3S6NgBE8gAK5WOpYCQ.jpeg</url>
            <title>Stories by Luis Mesquita on Medium</title>
            <link>https://medium.com/@luiscmesquita?source=rss-889f88d3cfd7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 04:32:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@luiscmesquita/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[Closures in Swift and how to break them…(Pun intended)]]></title>
            <link>https://medium.com/swlh/closures-in-swift-and-how-to-break-them-pun-intended-99c33593b9dc?source=rss-889f88d3cfd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/99c33593b9dc</guid>
            <category><![CDATA[closures-functions]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[xcode11]]></category>
            <dc:creator><![CDATA[Luis Mesquita]]></dc:creator>
            <pubDate>Sat, 30 May 2020 15:27:42 GMT</pubDate>
            <atom:updated>2020-06-06T15:14:04.017Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EAp6gpQNOV5pC-dJ4mBstg.jpeg" /></figure><p>When learning something new (it doesn’t matter if you’re a beginner or not), it is hard not to follow the “need to know it upside down” trap…And the trap can be imposed by yourself, and your personality type, but it is more often implicitly imposed by others (experts opinions, your colleagues, your teachers, you name it). Been there…done that… I’m 45 years old, and only a couple of years ago I’ve managed to start jumping over that trap. I guess for most of people, it comes with age. You start to mature and put things in perspective. I could write an essay about this topic…</p><p>Why this intro? It has nothing to do with Closures. But it has at the same time. I can only speak for myself here, but it is hard to find an article or a tutorial about Closures where the author assumes that you know nothing about it. It usually starts by saying that it is a hard topic, and once that is aside, soon the “trap” begins. The doors are open and the flood of possibilities and details around the topic will grab you and there’s nothing you can do. Not even to get rid of the trap. Don’t get me wrong…Most of the articles are great!!! I’m sure when I understand them, they will be even greater!!!</p><p>Enough of trying to be funny... My goal today is to try and keep it as simple as possible and to break down a Closure and its meaning and application, without the complication usually associated with it. The ideia for this article came when I was watching an Angela Yu’s video in her Udemy’s course. Actually credits to her. I will not copy what she said there, but the approach will be similar.</p><p>So, what is a Closure?</p><p>A Closure is a Function. First you need to realise that. Then you need to understand that is a Function, but most of the times is anonymous, and you don’t even need to name the parameters or to declare the return type. Also it “lives” inside another Function…And there’s when the confusion starts. At least for me. When I see a Closure’s syntax, I’m lost…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*O3My-ayYHen1Vo1pogjfqw.jpeg" /></figure><p>So what we’ll do now is to use a Function as an example, and break it down until it is a Closure. Step by step…</p><p>So we have a simple Function that will check if 2 words have the same number of Characters and then return a String with the answer. For that we have “firstWord” as a String, “secondWord” as a String, and instead of doing the verification inside the Function, we will have another parameter, “validation” to call another Function to do so. I know we could do it inside the Function, but to show you that we could call a Function as a parameter inside a Function we’ll do it this way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5aZzSyY5T-7RdZ7QY08AIQ.png" /></figure><p>Then we need to have the Function that will check if those 2 words have the same number of Characters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QsH0__V3k_BHshw-hhgINw.png" /></figure><p>Finally we need to call the initial Function to get our result as a String. So for “Mars” and “Venus”, we get “Different number of Characters”. Simple and working.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uP8kM9f9vs6pxo_670JbyA.png" /></figure><p>So the first objective here is to show you that it is possible (and really useful) to use a Function as an argument of another Function. The next steps will be to show you how to streamline the code until you get the minimal syntax of a Closure.</p><p>Let’s focus first on the Function that will be “transformed” into a Closure.</p><p>The first thing we need to do is to remove the name of the Function and to place the curly braces BEFORE the parameters and add the expression “<em>in”</em> to the syntax (Ignore the compiler erros for now).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/689/1*T7dfs0lmcrgjB3RqZF9qqQ.png" /></figure><p>Now, when we call the main Function, if we replace the <em>validation</em> parameter with this new piece of code (this is now a Closure), we get rid of the errors and we get the same result as before!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D4L93UJ0gYID_01MbXSTDQ.png" /></figure><p>That’s all good, but I don’t see much improvement or benefit by doing it this way. That is why we should keep going and rely on good Swift intelligence!!!</p><p>I’m sure you probably heard about Type Inference. So let’s try and use it here. If we remove the Type Declaration on parameters <em>word1</em> and <em>word2</em>, Swift still gets that must be a String and move on. And while we’re there, why don’t we do it also with the <em>Return Type</em>?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*1pk0r15p0y3HGWrbyqy0yQ.png" /></figure><p>Great! Still working and less code needed to run our Function! and we are still getting the same result “Different number of Characters”</p><p>With Closures, the compiler is smart enough to work with anonymous parameters. In Swift, if you want to call the first parameter of a Function you can simply use the dollar sign <em>$</em> and zero <em>0</em>, <em>$0</em>. The second parameter <em>$1</em>, and so on if needed. If we apply it to our syntax, we can streamline it even further.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*HUJS5Av5HZdtVpAltqqj9Q.png" /></figure><p>Do you want to reduce it even more? You’re in luck, because there is a rule in Swift, that will help you out to achieve that. If the last parameter of a Function is a Closure, you can omit the parameter name when calling it. More! You can even trailing it at the end of the Function you’re calling!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*nshV-JJzWPdeZMYt3X7BiQ.png" /></figure><p>So, let’s resume what we did. We started like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XM4CMyk4OgxhNyFL40Y53g.png" /></figure><p>And ended up like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IAw9U94uDwo4f4RY1DjZgQ.png" /></figure><p>If you look closely, what we did here was to pass the external functionality to the body of our call!!!</p><p>It might not seem like a great improvement, syntax wise. You can also argue that this could be done inside a Function, without the need of another Function. You’re right. But let’s get back to the beginning of the article. The main goal here is to simplify a concept and make it as easier as possible to follow. Because if you understand these simple steps and its meaning, you are much closer to understand when things get a lot more complicated. At least works for me. I just hope that it can help you as well!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99c33593b9dc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/closures-in-swift-and-how-to-break-them-pun-intended-99c33593b9dc">Closures in Swift and how to break them…(Pun intended)</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Where I’m at, after 4 months of learning iOS Development (5 Ups and 5 Downs)]]></title>
            <link>https://medium.com/@luiscmesquita/where-im-at-after-4-months-of-learning-ios-development-5-ups-and-5-downs-4a2a891dd5f?source=rss-889f88d3cfd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/4a2a891dd5f</guid>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[xcode11]]></category>
            <dc:creator><![CDATA[Luis Mesquita]]></dc:creator>
            <pubDate>Fri, 01 May 2020 16:01:18 GMT</pubDate>
            <atom:updated>2020-05-01T16:01:18.507Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7uSWocjTYAkFcs-0VkIoYg.png" /></figure><p>I started this journey more or less in the beggining of 2020. The first 2 months I did it all by myself. I went through <a href="https://books.apple.com/pt/book/intro-to-app-development-with-swift/id1118575552?l=en">Apple’s Book App Development With Swift</a> and an Udemy course <a href="https://www.udemy.com/share/101WsWAkUadl5WRQ==/">iOS 13 &amp; Swift 5 — The Complete iOS App Development Bootcamp</a> for the basics and to start to read and listen about the concepts and terms used. Then, middle February I started an online courseat the <a href="https://devslopes.com/">Devslopes Academy</a>.</p><p>My small programming experience remounted almost 20 years ago with VBA for Excel.</p><p>After 4 months, I would like to share where I’m at now. I’ll do it by presenting 5 “achievments” and 5 “not so good sensations” that I’m feeling every day. I’ll start with the “negatives” to end the article with a positive vibe!</p><h3>“Down #1 — The more I know, the less I know”</h3><p>I never felt overwhelmed with all the stuff I needed to learn in order to be a developer. I knew that I had to learn a lot, even though I was totally fine doing it with a step-by-step approach. But know that I know a lot more, I start to realise that the amount of information that one needs to know to be a developer it’s even bigger than I supposed. But, I’m still feeling ok with that. One small step at the time.</p><h3>“Down #2 — The freakin’ syntax”</h3><p>I made some progress here of course (look at the “ups”, in the next section of the article), but I’m still struggling so much with the syntax. Even some of the basics. I supposed that I would be much more comfortable by now. But I’m not, and sometimes it’s a bit frustrating, getting back to the documentation just to know how to perform an Array iteration…</p><h3>“Down #3 — The StackOverflow trap”</h3><p>Don’t get me wrong. I never, never would be able to finish some of my assignments without StackOverflow. But at the same time, it is a trap. As soon as I find the solution for my problem there, bang, I just use it and stop thinking on a solution myself. I’m not lazy. Far from it. But when you have a lot to deal with it, putting a “done checkmark” on something is tempting…But it is also a missed opportunity to grow your knowledge.</p><h3>“Down #4 — UIKit vs Swift UI”</h3><p>I totally understand that Swift UI is not there yet. It’s still missing some fundamentals, and it only works with iOS/iPadOS 13. I get why everyone’s saying that one should learn UIKit like as if Swift UI doesn’t exist. But I’m keep thinking that I’m spending a lot of time learning UIKit, and right afterwards, I need to go back to the beginning and then learn Swift UI… Just by writing this paragraph I start to feel some itches…</p><h3>“Down #5 — Not knowing how to write efficient code”</h3><p>Sometimes after solving a problem, I try and find a solution online just to compare. More often than not, the results are way, way far from what I’ve written. And it usually seems that my code is a lot more intricated and less efficient. I‘m so happy of being able to solve the issue, and then I see something cleaner and simple. It’s frustrating and brings me the feeling that I’ll never get there…</p><p>Enough of bad vibes. Let’s look at the Positives…</p><h3>“Up #1 — At my fingertips”</h3><p>Previously I’ve mention the syntax, and the struggle with it. But sometimes I find myself writing code without thinking and using autocompletion like second nature. And it feels good! That nice sensation of doing something intuitively. Those “rare” moments are unquestionably a motivation to keep going and a reenforcement that I’m on the right path.</p><h3>“Up #2 — Auto-Layout confort”</h3><p>When you begin to deal with AutoLayout, sometimes it seems that it doesn’t matter what you do, you can’t rid of those red lines. Even worth, you solve an issue, and with that action you break something else. It’s a nightmare. Until it isn’t. After a while everything starts to make sense, and once you get the logics of position and size and its relationships, those red lines are not an issue anymore and when you’re done with the UI, they are simply gone!</p><h3>“Up #3 — Understand and customize someone else’s code”</h3><p>I told you about the StackOverflow trap. But slowly you start to understand the code that you found there and how to customize it to fit your own needs. I’m not saying that is as satisfying as doing it by yourself from scratch, but it feels good anyway. Oh yes it does!</p><h3>“Up #4 —Official Documentation first”</h3><p>Nowadays I find myself going to Apple’s documentation and XCode Help before I start googling the answer. Before I didn’t have enough knowledge to understand the documentation. Unfortunately, technical documents are only for those who have at least a grasp of what to look for. And until you get there it is almost pointless. I can now say, that I’m “there” a lot of times.</p><h3>“Up #5 — Solving a problem”</h3><p>I’m a sports guy and I’m completely familiar with the endorphin and chemical reactions that happen during and after exercise. It becomes an addiction. A good one. I’m not a psychiatrist or a neurosurgeon, but I’m sure that the same happens when solving a coding problem or a challenge. It also becomes addictive. I just hope that it is as good an addiction as the sports related one…</p><p>And that’s it. I don’t know what my ups and downs will be in four months. I just hope that they’re different…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a2a891dd5f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Singleton in Swift, or “How a beginner saw the light…”]]></title>
            <link>https://medium.com/@luiscmesquita/singleton-in-swift-or-how-a-beginner-saw-the-light-b5e2c923946b?source=rss-889f88d3cfd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/b5e2c923946b</guid>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[singleton]]></category>
            <dc:creator><![CDATA[Luis Mesquita]]></dc:creator>
            <pubDate>Fri, 17 Apr 2020 16:44:10 GMT</pubDate>
            <atom:updated>2020-04-18T07:35:53.745Z</atom:updated>
            <content:encoded><![CDATA[<p>In my previous articles I’ve been mentioning that I’m beginning my iOS Development journey. With that comes a lot of that overwhelming feeling, “this is more that I can possible deal with…”. But step by step you start to see the light. And I like to share one of those “lighted” moments with you.</p><p>I’m participating in a “online coding Program/Academy” where we have a lot of assignements to do, with the complexity being increased as time goes by. In my last project, we had to build a sort of an online store (pretty rudimentary), just to practice Protocols, Delegates, TableViews, CollectionViews and passing data between those Views. All well and good, but what made my day was the introduction of the concept of a Singleton (I don’t know if there is the plural for that word…🤓).</p><p>I’d like to state first hand that what I’m about to explain, might be simplistic, and tailored only for beginners. It might even not be suitable for more complex projects. But I still don’t know that. I’m a beginner. So I would like to share only what I know…That being said…let’s start.</p><p>First of all, let’s set the stage. We have a couple of Scenes and it doesn’t really matter which type. It can be a Scene with a <em>CollectionView</em>, a simple one with just a <em>ViewController</em> and another with a <em>TableView</em>.</p><p>If you have more than one <em>Scene</em>, most likely you’ll want to pass information between each <em>Scene</em>. You might have the need to populate the <em>TableView</em>, based on information selected in the <em>CollectionView</em> or vice-versa. And there are ways to do it, but this article is not about that.</p><p>What we want to address is the need to have a certain piece of information, central to your project, that can be updated from any screen. It will not impact the other screens per se, but a global object instead.</p><p>A good example for that, is a Cart in a Store like App. You might have the need to update the Cart from whichever screen you are, without the worry of sending the information to the screen itself, in order to update the Cart.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/1*1vecNhfKoxF7wWPTRItNKw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/1*mwbLFbVSziocxLaLeRarWg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/834/1*Ow4sM6Hc1bpN8DCJZb6GHg.png" /></figure><p>As you can see from the images above, the Cart “object” can be updated from each one of the screens (by tapping the “<em>Heart</em>” <em>Button</em> on the first one, by changing the <em>Quantity</em> in the middle one and from the “<em>Add To Cart”</em> <em>Button</em> in the last one).</p><p>And one easy way to that is by using a Singleton.</p><p>First of all, you need to create one. Just create a <em>Class</em> and then inside it, create a <em>Static Constant</em> with the same type of that <em>Class</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*edVJI2AqITI6Hs5bOFvT-w.png" /></figure><p>Next you create the <em>Properties</em> and <em>Methods</em> needed for that global object. In this case, a <em>Property</em> called <em>Cart</em>, and then all the <em>Methods</em> needed to update the <em>Cart</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hf-SxZkYbNhDiQpQ63l-Jw.png" /></figure><p>We’ll not go into the details of each method because it is contextual for each project. I just want to show what you need to do.</p><p>When you need to call those <em>Properties</em> or <em>Methods</em> from inside each Screen, you just need to do it by invoking the <em>Class</em>, <em>Constant</em>, <em>Method</em> or <em>Property</em>. And then you have access to that global object, as follows:</p><p>From inside the <em>CollectionViewCell</em> to update the <em>Cart</em> when the “<em>Heart” Button </em>is tapped:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AhvhSvpIy2vLcoMB6NfJJQ.png" /></figure><p><strong>DataService</strong> is the <em>Class</em>, <strong>instance</strong> is the <em>Constant</em> and <strong>updateCart</strong> is the <em>Method.</em></p><p>From the <em>Checkout TableViewCell</em> when the <em>Stepper</em> changes the quantity:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iCI4PIiXc_ZAWKzI2fJkAA.png" /></figure><p><strong>DataService</strong> is the <em>Class</em>, <strong>instance</strong> is the <em>Constant</em> and <strong>updateCartFromCheckout</strong> is the <em>Method.</em></p><p>And from the <em>Details View</em> when the “<em>Add To Cart” Button</em> is tapped:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BBBbs8apSZQunctbdIwe6g.png" /></figure><p><strong>DataService</strong> is the <em>Class</em>, <strong>instance</strong> is the <em>Constant</em> and <strong>updateCartFrom Details </strong>is the <em>Method.</em></p><p>If you look closely, the methods called in each Scene (look at the green arrows above), are exactly the same that were created inside the <strong>DataService</strong> <em>Class</em> created in the beginning.</p><p>And so, you can update the <strong>Cart</strong> <em>Property</em>, from every screen, and the updated value of that property is available to be presented, without the need of passing data between Scenes.</p><p>In a nutshell:</p><ol><li>Create the <em>Class</em> and inside that <em>Class</em>, create a <em>Static Constant</em> of the that same <em>Type;</em></li><li>Inside that <em>Class</em>, create all the <em>Properties</em> and <em>Methods</em> needed for your logic;</li><li>Call the <em>Methods</em> from everywhere in your project with following syntax: <em>Class.Constant.Method</em> or <em>Property</em></li></ol><p>Again, there might be some more advanced stuff that can be done with a Singleton. Or even it might not be optimal for every scenario. I’m not there yet. But for now, it helped me simplifying my code.</p><p>I hope it helps you as much as helped me.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b5e2c923946b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scroll Views in XCode 11]]></title>
            <link>https://medium.com/macoclock/scroll-views-in-xcode-11-d1600540e0d2?source=rss-889f88d3cfd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/d1600540e0d2</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[xcode11]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Luis Mesquita]]></dc:creator>
            <pubDate>Sun, 05 Apr 2020 15:47:30 GMT</pubDate>
            <atom:updated>2020-05-08T16:48:57.139Z</atom:updated>
            <content:encoded><![CDATA[<h4>Learn how to efficiently display scrollable content in iOS apps</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*py35pzkUuacUPQ4D" /><figcaption>Photo by <a href="https://unsplash.com/@grohsfabian?utm_source=medium&amp;utm_medium=referral">Fabian Grohs</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As a new programmer (I guess not even there yet), I’m constantly finding answers for my “quests”, that do not completely conform with the actual version of iOS or XCode. One of these examples is the way that Scroll Views are now implemented by default in XCode 11.</p><p>If you try and follow along with the online tutorials and StackOverflow answers about ScrollView implementations, I guess 90% of those are still related to the old way of doing it. It is not big of a difference conceptually, but it’s enough to get your head banging against the wall if you start to follow along with those tutorials and guides in XCode 11. I’ve been there and done that. So I thought, “I must figure this out, no matter what”. So what you are about to see, is 90% equal to what you can find all over the internet, but with the addition of the small “big” change introduced in XCode 11.</p><h3>What is a Scroll View?</h3><p>A Scroll View for those who are even more beginners than I am, as the name implies, is what allows you to scroll through content in your iOS device. You can find them everywhere, whether it is a vertical scroll, whenever your content is presented as a list, (Email App, the Files App, Facebook, you name it); a horizontal scroll (less common but you can find it in some content Apps where the Categories are presented horizontally, check IMDB for instance); and both horizontal and vertical (the common example to present here is the Photos App, where you can double click on a photo to zoom and then you scroll the photo in both directions back and forth).</p><p>For that to work in Auto Layout, the Scroll View object needs to have a size (<em>width and height</em>) and a placement in its Superview (<em>x</em> and <em>y</em>). For the purpose of this article let’s call this, a <strong>Frame Layout</strong>. It also needs to know the width and height of the content to present. Let’s call it, a <strong>Content Layout</strong>. Remember those two terms and what they mean in bold, <strong>Frame Layout</strong> and <strong>Content Layout</strong>. Read this paragraph again if it is not clear in your mind yet.</p><h3>So what is the difference?</h3><p>As I mentioned in the beginning of the article there is a small difference introduced in XCode 11. The theory and concepts are the same, but the default implementation of Auto Layout in Interface Builder to prepare a Scroll View to act as pretended is a bit different. And it is for better if you ask me because it makes the concept explained before, much clearer and less prone to errors when building your Interface. But… If you are not aware of that change, you try and use all those tutorials out there, it simply won’t work…</p><p>When you drag a Scroll View to your ViewController you need to deal with the constraints in order to conform to the AutoLayout rules for the Scroll View (remember the <strong>Content Layout</strong> and <strong>Frame Layout</strong>?). But before, that was not explicit in the steps you needed to perform to achieve it. From my limited experience, you had to deal with a lot of constraints errors, before you got rid of all those red lines in IB. I’m not gonna follow that path, because as said before if you want to do it that way you can find a lot of articles out there.</p><p>Now, when you drag a Scroll View to Interface Builder, by default you find a couple of new options already prepared for you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UEs4xWV0dyp60be5A7dOVQ.png" /></figure><p>Remember the <strong>Content Layout</strong> and <strong>Frame Layout</strong>? Now, the guides are there for you and can be “easily” used to help you out setting your ScrollView to act as needed.</p><p>By the way, if want to use the “old way”, just uncheck the Content Layout Guides in the Size Inspector.</p><p>After you set the Constraints for those two Guides you are all set to build your UI, using Scroll Views, weather you want them to be dynamically sized or with a fixed size. Let’s see how can we do that:</p><h3>Setting the Constraints using Content Layout Guide</h3><p>So do you still remember the meaning of <strong>Content Layout</strong>? It is the size and position of the content you want to present in the Scroll View. It gets easier as you do it more often but is important to understand it from the beginning. In the picture above a Scroll View is already placed in its Superview. If you haven’t done already do it now in your project to follow along.</p><p>After placing the Scroll View in the View Controller, just drag the edges to match the edges of the Superview (if you want you can also set it to match the Safe Area. It’s up to you and your preference, but the setup is the same). Then just Add New Constraints, pinning the Scroll to whichever edges you like. In the example, we are pinning it to the edges of the Superview on all sides.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FH7fkiQRxOesGn73bQ8U6A.png" /></figure><p>Although it seems you are taking care of the Size and Placement of the Scroll View — <strong>Frame Layout</strong> — you are just giving some space to drag some content to the Scroll View. As you can see, we still have some red lines and AutoLayout issues.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kw9UntCz2eHpRx4-kK9zhQ.png" /></figure><p>We’ll deal with that soon, but the problem is that Interface Builder still doesn&#39;t know the size of the content to be presented and consequently the size of the Scroll View.</p><p>A Scroll View is “invisible” as a UI object. You need to place some content inside it to be presented. And the easiest way to make it dynamically sized is to add an object that automatically adjusts its size based on the content inside. That’s why we should use a Stack View. Alternatively, if you want to use a fixed size view, you can just drag a “regular” View Object and set its width and height as you please. In the example, we use a Vertical Stack View.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f3Xfnt297ICFCVf1QzdUxA.png" /></figure><p>Our immediate response (at least mine), would be to start and drag the edges to match the Scroll View, but we’ll do that in a different way.</p><p>On the Document Outline CTRL+Drag the Stack View entry to the Content Layout Guide. Remember what are we trying to do? We want to set the size of the Content to be presented! After you Drag a popup will appear. Press Shift and select the Leading/Top/Trailing/Bottom constraints. We are saying to Interface Builder that we want our content to match the placement of the Content Layout Guide. In other words, we are setting the Content Layout position.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*foxBssz_WBCJ6OFtIozUug.png" /></figure><p>As we didn’t drag the edges of the Stack View, Interface Builder assumes what was there instead. To force the Scroll View to go to the edges of our screen we should edit the Constraints. Select the Stack View in the Document Outline, and go to the Size Inspector. You will see the Constraints you just built. Press Edit in each one of them and set it to 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R87c8OzEkQzh6RJFo5lggA.png" /></figure><p>We’re not there yet. You still have red lines and a “small” Stack View in the middle of your UI. But bare with me. It will all make sense soon. Actually is making sense now already. The Interface Builder still doesn’t know the size of the Content you want to present. That’s why it is giving you the red lines.</p><h3>Setting Constraints using Frame Layout Guide</h3><p>In this example, we just want to scroll vertically. If you think about the act of scrolling only vertically, the leading and trailing edges don’t move. We only have “movement” from bottom to top and vice-versa. Meaning that we don’t have width variations. So we need to tell Interface Builder that our content should be equal or lesser than the Scroll View width. Makes sense?</p><p>For that, CTRL+Drag the Stack View to the Frame Layout Guide entry as we did before. In this case, just select Equal Widths.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PViVhuUQOhvsDoIl6-DGfg.png" /></figure><p>Most probably, XCode created a less than 1 proportion based on the size of the Stack View you had.</p><p>As we did before, go to the Size Inspector and set the Multiplier parameter to 1. I mean, do this, only if you want the content to match the width edges of the Scroll View. If you need it smaller than that, adjust it accordingly or let be as is.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ne4TPglpL64u3t86HZ-Yaw.png" /></figure><p>Wait! But why are we still getting red lines???</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T7bDy1cUboz6KtdvzHVqAg.png" /></figure><p>Any guesses why? You’re right because the Stack View is waiting for content and therefore to have a size!!!</p><p>As soon as you place any View inside the Stack View, its size will automatically adapt and the red lines are gone!!! You don’t believe me? So go on, and drag a Label inside the Stack View. As soon as you do that, the red lines are gone!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8AqwI46_s-K0t9sQGZLYAw.png" /></figure><p>Now to see the horizontal scroll in action, you just need to add enough text to the Label, to make it bigger than the screen. You can do that by selecting the Label and updating the text parameter with a big enough text. Also don’t forget to set the Number Of Lines parameter to 0, in order for the label adjust the number of lines automatically.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DiV6SXS3V60x8zpLQsU5sQ.png" /></figure><p>As soon as you do it, you can then select the Stack View in the Document Outline, and scroll through the content to see it in action! You can also see the size of the content by following the blue lines.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dOmq0yVxhd7SaipJgoKKMA.png" /></figure><p>I seriously doubt that you want your App to conform to the previous screenshot appearence 😀. I did that on purpose because now you can see what it means to use the Safe Area in Auto Layout. At the beginning of this article we pinned the Scroll View to the edges of the Superview. Go back to see that if you have any questions about it. That is why the text stretches all the way up, left and right.</p><p>But if you select each Scroll View Constraint in the Document Outline, you can now change the reference to the Safe Area instead of Superview. Make sure you have the Constant parameter set to 0 in the top and bottom, to 20 on the leading and to -20 on the trailing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9-bw0HOZJBq5BByZWhJDNg.png" /></figure><p>After those changes you should end up with something similar to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LmpNZ1fMXzBqvZZlUJGxGg.png" /></figure><p>If you then run your App in the Simulator, you should see the text scrolling smoothly, top to bottom!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*hNHs_QKfgiRmBfWd5oAnxQ.png" /></figure><p>I really hope that you find this useful, and from now on, Scroll Views in Xcode 11 won’t scare you as much. I mean if you are a beginner like me, of course…🤓</p><p>You can find the project here:</p><p><a href="https://github.com/LuisMesquita-006/ScrollViews.git">https://github.com/LuisMesquita-006/ScrollViews.git</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d1600540e0d2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/macoclock/scroll-views-in-xcode-11-d1600540e0d2">Scroll Views in XCode 11</a> was originally published in <a href="https://medium.com/macoclock">Mac O’Clock</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to deal with different Region’s number formats in Swift]]></title>
            <link>https://medium.com/swlh/how-to-deal-with-different-regions-number-formats-in-swift-933fbbf31931?source=rss-889f88d3cfd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/933fbbf31931</guid>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[numberformatter]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <dc:creator><![CDATA[Luis Mesquita]]></dc:creator>
            <pubDate>Sun, 22 Mar 2020 16:58:54 GMT</pubDate>
            <atom:updated>2020-04-22T12:39:02.657Z</atom:updated>
            <content:encoded><![CDATA[<p>When building a simple Calculator App as my first App, I came across an issue that was never a concern when I started to code the App: numbers, most surely have different presentation formats, based on the user’s phone Region Settings!!!</p><p>That can impact not only the UI/UX of course, but also the way you write your code. You don’t want to have a String that cannot be converted to a Double and therefore cannot be used in the calculation. There might be some other ways to solve this, but the one that I found the easiest is by using the NumberFormatter Type. Swift make it simple and deal with all the transformations for you!</p><p>So how to do it? We’ll use a simple example in Playgrounds.</p><ol><li>First we declare a variable with the NumberFormatter Type:</li></ol><p><em>var number = NumberFormatter()</em></p><p>2. Then using some of the properties of that Class, we can format it as we please:</p><ul><li>first let’s use the user’s current Region to set the general format of our variable.</li></ul><p><em>number.locale = .current</em></p><ul><li>it can have a lot of Styles, like currency, ordinal or percentage. For the example let’s keep it with decimal:</li></ul><p><em>number.numberStyle = .decimal</em></p><ul><li>setting it to true, we are accepting the format the current region has for the thousands separator. Alternatively we could use the commented line, setting the group separator ourselves:</li></ul><p><em>number.usesGroupingSeparator = true</em></p><p><em>//number.groupingSeparator = “ “</em></p><p>3. Then let’s deal with the user’s input from the UI. That value comes for instance from a Text Field, is of type String, and we place it into a variable:</p><p><em>var userInput= “12000.02”</em></p><p>Just a String, with no spaces, and with a dot as the decimal separator.</p><p>4. The “tricky” part here is that we need to convert the String to a NSNumber in order to use the <em>string()</em> method of the NumberFormatter type. For that and to remove the complication everytime we need to use this, let’s make it as a function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FmkQXN5Et4mvccRiy6l4RA.png" /></figure><p>5. If we pass our User’s input through the function, we have the correctly formatted and useful result as a String, ready to be presented in the UI:</p><p><em>var result = formatResultForRegion(basedOn: userInput)</em></p><p><em>print(result)</em></p><p>In my case it will print <em>12 000,02</em> because I’m Portuguese and that’s how we divide the thousands (with a space), and decimals (with a comma).</p><p>6. Let’s just change some of the properties of our NumberFormatter variable and see what we would get:</p><p><em>number.locale = Locale(identifier: “pt_BR”)</em></p><p><em>number.numberStyle = .currency</em></p><p>As you can see we can also force the region to be whichever we want to. Just set it to the region you want. You can find the guide for the correct identifiers <a href="https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPInternational/LanguageandLocaleIDs/LanguageandLocaleIDs.html">here</a>.</p><p>Based on those changes if you call the function again the result will be:</p><p><em>$R 12.000,02</em></p><p>We changed the region to Brazil and the style to currency!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hd4xb05BMB6N-jtFYY8lgQ.png" /></figure><p>And that’s it! To know more about the NumberFormatter Type, its properties and methods, go <a href="https://developer.apple.com/documentation/foundation/numberformatter">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=933fbbf31931" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/how-to-deal-with-different-regions-number-formats-in-swift-933fbbf31931">How to deal with different Region’s number formats in Swift</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Customizable UI Controls in the Atributes Inspector — UIKit]]></title>
            <link>https://medium.com/@luiscmesquita/customizable-ui-controls-in-the-atributes-inspector-uikit-c2ecf68be0f?source=rss-889f88d3cfd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/c2ecf68be0f</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios-development]]></category>
            <category><![CDATA[uikit]]></category>
            <dc:creator><![CDATA[Luis Mesquita]]></dc:creator>
            <pubDate>Sun, 15 Mar 2020 19:43:48 GMT</pubDate>
            <atom:updated>2020-03-15T19:43:48.934Z</atom:updated>
            <content:encoded><![CDATA[<p>Customizable UI Controls in the Atributes Inspector — UIKit</p><p>Once I understood what an Extension is, and what it tries to address (a lack of some functionality or native atributte in a View), I started to wonder, “would it be possible to change and see those new attributes in Interface Builder?”. I bet it would, but how?</p><p>Well using @IBInspectable/@IBDesignable it is possible! Not only to apply the changes but even to see them in Interface Builder!</p><p>So it goes like this:</p><p>Step 1: As an example we’ll add the cornerRadius, borderWidth and borderColor attributtes to the UIView Class. Using @IBInspectable, those new settings will appear in Interface Builder:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jo60srBBePOAmShnuOJD3Q.png" /></figure><p>Step2: Then you need to create a new Class for the Views you want to have “live feedback” in Interface Builder. For that you use @IBDesignable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tHTaAzSfMtDj9SghgWddzg.png" /></figure><p>Step3: In the Storyboard, change the Class Type of that particular View (to match the ones you’ve just created), in order to understand and receive those new attributes and show them in Interface Builder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yOKTa96J5qQsaQqGODOh2g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z5N_NEqTGpwu3QuvEfOK0w.png" /></figure><p>Step4: Change what ever you need and see the results “live”!!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*McUQzyOfc4aLanU8lsrSJQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y3u3F1huw1jPuHa_NTtxag.png" /></figure><p>If you make changes to a view, but did not created a new Class with @IBDesignable, you can only see those changes in Simulator…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DTyJv1bUrBKwEIt9OW1cgw.png" /></figure><p>For me, the only downsize of this approach, is that, those new parameters will always appear in top of the Attributes Inspector, independently of the View that you select. It is OK, because you can make changes anyway, but the “cherry on the top of the cake” would be, to only appear if you can see it in the Interface.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2ecf68be0f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use a Collection of Buttons triggered by the same IBAction]]></title>
            <link>https://medium.com/@luiscmesquita/how-to-use-a-collection-of-buttons-triggered-by-the-same-ibaction-5dee9e26e6e0?source=rss-889f88d3cfd7------2</link>
            <guid isPermaLink="false">https://medium.com/p/5dee9e26e6e0</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[uibutton]]></category>
            <dc:creator><![CDATA[Luis Mesquita]]></dc:creator>
            <pubDate>Mon, 09 Mar 2020 15:04:19 GMT</pubDate>
            <atom:updated>2020-03-09T15:04:19.733Z</atom:updated>
            <content:encoded><![CDATA[<p>A simple way to simplify your code is to have…less code as possible. As a beginner, I’m still struggling to achieve that, but I want to share something that I found useful in order to do it.</p><p>Say you have a need for a function that can be triggered by more than one button, getting different results based on the triggered button.</p><p>I found 2 simple ways to do that: using the <em>tag</em> attribute or the <em>currentTitle</em> attribute of UIButton.</p><p>If you want to use the tag approach, just assign a diferent tag for each button in the Attributes Inspector.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*jJDMo6uXaFWfiJiRmm-Htw.png" /></figure><p>Then just drag the IBAction you want to perform when the button is tapped, to each button (using the circle in the line number on Xcode) .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZLODC41VDg_sPdMr8W9P-A.png" /></figure><p>If can then use for instance, a Switch Statement on the <em>tag</em> attribute of the Button Collection you’ve just created, having different behaviours based on the button pressed.</p><p>Another way to do it is to use the <em>currentTitle</em> attribute. In this case you can even get the title of the button and pass it to a function to perform whatever you want with it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tGlBRENyGtkBeBaUI-DDqg.png" /></figure><p>And that’s it! Pretty basic I know, but Hey I’m just starting…and from what I’ve learned so far, the hardest part in the process is to keep it simple!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5dee9e26e6e0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>