<?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 Kleyton Santos on Medium]]></title>
        <description><![CDATA[Stories by Kleyton Santos on Medium]]></description>
        <link>https://medium.com/@kleyton?source=rss-653377a36269------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*z9XsUBaqjc4pGcE0b8ho4Q.jpeg</url>
            <title>Stories by Kleyton Santos on Medium</title>
            <link>https://medium.com/@kleyton?source=rss-653377a36269------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 06:39:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kleyton/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[How to detect memory leaks on UIViewControllers using break points in Xcode]]></title>
            <link>https://medium.com/@kleyton/how-detect-memory-leaks-on-uiviewcontrollers-using-break-points-in-xcode-72e23810eb63?source=rss-653377a36269------2</link>
            <guid isPermaLink="false">https://medium.com/p/72e23810eb63</guid>
            <category><![CDATA[conditional-breakpoints]]></category>
            <category><![CDATA[memory-leak]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Kleyton Santos]]></dc:creator>
            <pubDate>Fri, 08 Apr 2022 03:08:58 GMT</pubDate>
            <atom:updated>2022-04-08T12:03:05.505Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*wuhvhgx_m1_bxVShFjK22A.png" /></figure><p>Today, i&#39;m thinking how can I detect easily memory leaks in our ViewControllers, some developers uses the &quot;Instruments Tool&quot; from apple to analyze memory leaks in our app, other ones just add a <strong><em>deinit</em></strong> keyword with a print inside to make sure that the ViewController was purge from the memory like this way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/637e6c818db798e10c0f8fbbaecb1c04/href">https://medium.com/media/637e6c818db798e10c0f8fbbaecb1c04/href</a></iframe><p>Every single time that the Object is removed from the memory, we going to see this print in our log and this way we assurance that the object was purged from the memory.</p><p>Let&#39;s use some configurations of break point to test if an UIViewController has a Memory Leak. Look at this code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a56ee8b961665f66d0f6d407108037e/href">https://medium.com/media/6a56ee8b961665f66d0f6d407108037e/href</a></iframe><p>We have a <strong><em>SecondViewController</em></strong> with <strong><em>SecondViewModel</em></strong> as a dependency and in <em>viewDidLoad</em> method we instantiate our dependency and pass a closure that&#39;s using a strong reference of <strong><em>SecondViewController</em></strong>.<strong><em>self</em></strong>. This way we creating a cycle reference between our <strong><em>SecondViewController</em></strong> and our <strong><em>Closure</em></strong>.</p><p>Let&#39;s configure our dealloc symbolic break point.</p><p><a href="https://youtube.com/shorts/u_o2Bt3DadA">Adding a symbolic Break point Dealloc</a></p><p>Let&#39;s also add a viewDidLoad break point observer :</p><p><a href="https://youtube.com/shorts/Bhgyem9pK3U">Adding a symbolic Break point ViewDidLoad</a></p><p>The steps are kind of similar, we add a symbolic break point and we pass a function or method name in Objective C, and after we add two actions to notify our events, in our case we added a sound and a log like this:</p><blockquote>— — dealloc MemoryAlert@(id)[$arg1 description]@</blockquote><p>The <strong><em>@(id)[$arg1 description] </em></strong>will give to us the name of our UIViewController and the memory address. This way we make sure that the address showed in viewDidLoad should be the same on dealloc. The checkbox options as true <em>&quot;Automatically continue after evaluating actions&quot; </em>is just to not stop our execution.</p><p>Let&#39;s see a example:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FNakQ5rsB5UA%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DNakQ5rsB5UA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FNakQ5rsB5UA%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ac7037907651b083f944193d7e3aca2/href">https://medium.com/media/7ac7037907651b083f944193d7e3aca2/href</a></iframe><p>Our logs shows that aways when we call the <strong><em>viewDidLoad</em></strong> method, we create a new instance with new address memory and when we removes <strong><em>SecondViewController</em></strong> from screen, the <strong>ARC</strong> couldn&#39;t call <strong><em>dellaoc</em></strong> method because SecondViewController has a cycle reference inside the viewModel closure. Let&#39;s add a <strong><em>[weak self]</em></strong> in our closure and see what happens now:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FtD8cIGyjhpI%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DtD8cIGyjhpI&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FtD8cIGyjhpI%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2638a9be50a5df8853c4bacca86842d/href">https://medium.com/media/d2638a9be50a5df8853c4bacca86842d/href</a></iframe><p>After added the <strong><em>[weak self]</em></strong> in our closure, we guarantee that we have a weak reference from our UIViewController, so ARC can purge from memory if necessary. Also note that address memory on viewDidLoad log and the dealloc log are the same. :)</p><p>You can find the code used in this example <a href="https://github.com/kleytonandroid/MemoryLeaksExample">here</a>!</p><p>Thank you :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=72e23810eb63" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Is It Possible to Use SwiftUI Preview on UIViewController?]]></title>
            <link>https://medium.com/better-programming/is-it-possible-to-use-swiftui-preview-on-uiviewcontroller-b86425167765?source=rss-653377a36269------2</link>
            <guid isPermaLink="false">https://medium.com/p/b86425167765</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Kleyton Santos]]></dc:creator>
            <pubDate>Fri, 25 Mar 2022 21:09:18 GMT</pubDate>
            <atom:updated>2022-03-31T04:17:05.936Z</atom:updated>
            <content:encoded><![CDATA[<h4>Let’s find out!</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*oVFzAEb3rBhgMadg_okLkg@2x.png" /><figcaption>Source: Apple</figcaption></figure><p>In WWDC 2019, Apple surprised every developer by announcing a completely new framework called SwiftUI. It doesn’t just change the way you develop iOS apps. This is the biggest shift in the Apple developer’s ecosystem (including iPadOS, macOS, tvOS, and watchOS) since the debut of Swift.</p><p>In addition to creating views in a declarative way, you would now have a preview of your screen, updating each graphic modification included in your view. This increases your productivity as you no longer need to run your app with every change to see if everything is working as expected.</p><p>If you are still working with UIKit framework but you&#39;d like to use PreviewProvider from SwiftUI to increase your productivity, I&#39;ll give you some tips on how to make this possible.</p><p>Let&#39;s create an extension of UIViewController:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c673700879f607f50478460a70e2795/href">https://medium.com/media/7c673700879f607f50478460a70e2795/href</a></iframe><ol><li>The Xcode only builds this block of code if it is possible to import SwiftUI and if you are in debug mode. (optional)</li><li>Make the extension available to consumers with iOS 13 and above.</li><li>A private Preview struct conforms to the UIViewControllerRepresentableprotocol. We will use it to create a preview for our view controllers.</li><li>Struct has a dependency on UIViewController. For example, if we want a preview for the SettingsViewController, we will provide it to the Previewstruct.</li><li>makeUIViewController(context:) and updateUIViewController(_ uiviewController:context:) methods are required by UIViewControllerRepresentable protocol. Inside makeUIViewControllermethod, we return the view controller that we want to obtain a preview of. The latter method stays empty.</li><li>Create a convenient preview property that returns a Preview for a view controller.</li></ol><p>That’s all we need to add in order to support previews of any UIViewController in our apps. Let’s test what we’ve created.</p><h4><strong>Usage Example</strong></h4><p>Let’s create a simple ViewController HomeViewController.swift :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73f523dabc512e7ef4573627876e3c18/href">https://medium.com/media/73f523dabc512e7ef4573627876e3c18/href</a></iframe><p>On this HomeViewController.swift we have an optional HomeView, let&#39;s create a simple UIView with 3 labels and 1 button using view code for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af70b72b50d3579afe2abfc16f795765/href">https://medium.com/media/af70b72b50d3579afe2abfc16f795765/href</a></iframe><p>In our example, we isolated things related to views of our screen on a file HomeView.swift .</p><p>Ok, but what about our PreviewProvider ?</p><p>As default on SwiftUI, we can add it at the end of our view file. Let&#39;s add at the end of HomeView.swift :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41dfbda76288f09701468e79e82572d0/href">https://medium.com/media/41dfbda76288f09701468e79e82572d0/href</a></iframe><ol><li>Xcode will build this block of code only if it is possible to import SwiftUI and if you are in debug mode, this is the same reason we included on our UIViewController extension. (optional)</li><li>We provide a preview of our extension from a UINavigationController passing our ViewController as a rootViewController.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k0YmongoYAKBUTmcbRm_Iw.png" /></figure><p>And <em>voila!</em></p><h4>Some important things/tips…</h4><ul><li>The name of the struct Preview should have a “Preview” suffix, example: ViewControllerPreview, HomeViewControllerPreview, SomeViewPreview. If not, you are going to have a build error =/.</li><li>If the preview canvas is not presented, you can use the shortcut ⌘+Option + Return or do it manually.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DijtTjNRaUcaloXp7Jt13g.gif" /></figure><ul><li>To resume your preview you can also use a shortcut ⌘+Option+P.</li></ul><p>Ok, we learned how to implement Preview for our ViewControllers but I&#39;m super lazy and I don&#39;t want to add all the code manually on every single ViewController that I&#39;ll create, so let&#39;s create a <em>Code Snippet</em>!</p><p>To create a new Code Snippet you can use right click in any place inside a file and click on “Create Code Snippet”<em>. </em>Then you are going to give a name to your Code Snippet, add the block of code that is going to fill up on autocomplete, then add this block of code and add your tag for autocomplete, I&#39;ve used SwiftUIPreview in this example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f5001641b988c8b3e1cc606ca3f7e46/href">https://medium.com/media/5f5001641b988c8b3e1cc606ca3f7e46/href</a></iframe><p>It will work like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PyUxbFQ34o_9Z3MA5CWy6g.gif" /></figure><p>To learn more about code snippets and how to implement them, you can read this <a href="https://sarunw.com/posts/how-to-create-code-snippets-in-xcode/">article</a>.</p><h3>Resources</h3><p>The source code of the sample project is available on <a href="https://github.com/kleytonandroid/SwiftUIPreviewOnUiKit">GitHub</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b86425167765" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/is-it-possible-to-use-swiftui-preview-on-uiviewcontroller-b86425167765">Is It Possible to Use SwiftUI Preview on UIViewController?</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>