<?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 Hsin on Medium]]></title>
        <description><![CDATA[Stories by Hsin on Medium]]></description>
        <link>https://medium.com/@hsintzuyen?source=rss-e564bd53871f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dp18tW3jBexTe1tovEi0kg.png</url>
            <title>Stories by Hsin on Medium</title>
            <link>https://medium.com/@hsintzuyen?source=rss-e564bd53871f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 07:02:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hsintzuyen/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[Xcode12 Error: building for iOS Simulator, but linking in object file built for iOS,for…]]></title>
            <link>https://medium.com/@hsintzuyen/xcode12-error-building-for-ios-simulator-but-linking-in-object-file-built-for-ios-for-5e03603dd3ff?source=rss-e564bd53871f------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e03603dd3ff</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Hsin]]></dc:creator>
            <pubDate>Tue, 03 Jan 2023 16:44:14 GMT</pubDate>
            <atom:updated>2023-01-03T16:47:17.568Z</atom:updated>
            <content:encoded><![CDATA[<h3>Xcode12 Error: building for iOS Simulator, but linking in object file built for iOS,for architecture arm64</h3><h3>Workaround:</h3><p>This Xcode error can <em>temporarily</em> be neglected by setting the value of <strong>Excluded Architectures.</strong></p><p>Go to <strong>Build Setting -&gt; Excluded Architectures -&gt; </strong>Add<strong> “arm64” to </strong>Debug/Release with<strong> “Any iOS Simulator SDK”</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JLjDjQr-2UX1XbpE6-9IBw.png" /></figure><h3>Possible additional workaround:</h3><p>If the issue persists, set <strong>Validate Workspace</strong> to <strong>YES.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lcpDYKx-ZA4Flcv1zrWL6A.png" /></figure><p>Voila, Now the project should be able to build and run.</p><h3>Root cause:</h3><p>The reason behind this error can be referenced in <a href="https://developer.apple.com/documentation/technotes/tn3117-resolving-build-errors-for-apple-silicon"><strong>this technote</strong></a> written by Apple.</p><p>Basically, with the release of M1, Apple uses its Apple silicon with arm64 architecture. Starting Xcode 12, arm64 by default is included in the building architectures, but some of the frameworks/3rd party dependencies do not support arm64 running on simulators. Usually, we don&#39;t have access to the source code of the third-party dependencies to change the setting. That’s why for a temporary fix we exclude the arm64 architecture to run the project. But eventually, the above workaround is <strong>NOT</strong> the ultimate solution to this error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C90U_JjKhDj5bUZOb_rM8A.png" /><figcaption>Important note from Apple technote</figcaption></figure><p><em>If you think this is helpful, give me some claps 😌 any comments are welcome!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e03603dd3ff" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to successfully implement Model-View-ViewModel (MVVM) architecture pattern in iOS App…]]></title>
            <link>https://medium.com/@hsintzuyen/how-to-successfully-implement-model-view-viewmodel-mvvm-architecture-pattern-in-ios-app-8ef97657f180?source=rss-e564bd53871f------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ef97657f180</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[banking]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Hsin]]></dc:creator>
            <pubDate>Tue, 17 May 2022 11:56:32 GMT</pubDate>
            <atom:updated>2022-05-19T08:09:41.338Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to successfully implement Model-View-ViewModel (MVVM) architecture pattern in iOS App development</h3><h4>Overcoming the drawbacks of MVP and MVC design patterns to deliver a superior product</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mxbc07Dwgz-D9kNcDCCweA.png" /></figure><h4>Intro</h4><p>Model-View-ViewModel (MVVM) has become increasingly popular in recent years, as more developers have realised that using Model-View-Controller (MVC) results in a challenge: massive view controllers. MVCs often mean that view controllers carry excessive responsibilities, leading to tight coupling and an increase in maintenance costs.</p><p>At DBS, we adopt MVVM in several consumer apps, such as PayLah! in Singapore and Card+ in Taiwan. In this post, I will be using Taiwan’s Card+ app as an example to highlight how we implemented MVVM.</p><h4><strong>The Importance of Architecture Patterns</strong></h4><p>Architecture Patterns are used widely in software development; applying a relevant architecture pattern makes software maintainable, manageable, testable, reusable, understandable, and flexible. It is crucial to ensure that the software we develop is maintainable, as we maintain our software for more than 80% of its lifetime. One of the ways to ensure easy maintenance is through the adoption of architecture patterns.</p><h4><strong>Main Concept of MVVM</strong></h4><p>MVVM binds user interfaces to Model classes through View Model. There are three main components of MVVM:</p><ul><li><strong>Model</strong>: A structure that manages data;</li><li><strong>View</strong>: Displays visual elements and formatted data for users. It also receives user interactions, such as taps, clicks, and keyboard inputs; and</li><li><strong>ViewModel</strong>: The mediator between Model and View. ViewModel retrieves raw data from Model and formats it into data that can be displayed to users (View)</li></ul><p>Once a user triggers an action, the interaction between these three layers usually happens this way:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1-Z734M2-OO0AF8wiRjylg.png" /><figcaption><em>Figure 1: User triggers an event -&gt; View/ViewController -&gt; ViewModel -&gt; Model -&gt; View/ViewController</em></figcaption></figure><p>In MVVM, ViewModel formats data received from service and model types. After formatting data, ViewModel passes it to ViewController, which is then presented to the users. This data flow requires a callback mechanism to bind ViewModel and ViewController data, which we can achieve through some methods such as delegation and closure.</p><h4><strong>Data Binding: Delegation</strong></h4><p>Delegation is also known as delegate pattern, which is a design pattern that allows an object to communicate with its owner. Delegation broadly appears in Apple’s APIs, including <em>UITableViewDelegate</em>, <em>CLLocationManagerDelegate</em>, and <em>UICollectionViewDelegate</em>. The most common way to set up delegation is by using delegate protocols, which is what Apple’s APIs use.</p><ol><li><strong>ViewModel</strong></li></ol><p>We define a protocol with a name that ends with <em>-delegate</em>, such as <em>HomeViewModelDelegate</em>. Inside <em>HomeViewModelDelegate</em>, we declare the method that ViewModel needs to call to update ViewController. In Figure2, we declare a method <em>didFinishFetchingData</em> inside <em>HomeViewModelDelegate</em>, which we would trigger when ViewModel finishes fetching the required data.</p><p>Next, we declare a property named <em>homeViewModelDelegate</em> and conform it to the <em>HomeViewModelDelegate </em>protocol in ViewModel. In Figure 2, we declare <em>homeViewModelDelegate</em> conforming to <em>HomeViewModelDelegate</em>.</p><p>Lastly, we call the delegate method by writing <em>homeViewModelDelegate.methodName</em> to update ViewController. In Figure 2, we call <em>self.homeViewModelDelegate?.didFinishFetchingData(data)</em> upon receiving the data. This is to notify the delegate that the data is ready to be used.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4a72519ce8d79e51bec264f9e512e1a/href">https://medium.com/media/d4a72519ce8d79e51bec264f9e512e1a/href</a></iframe><p><strong>2. ViewController</strong></p><p>Now, we move on to ViewController, the delegate of ViewModel.</p><p>To receive updates from ViewModel, ViewController must assign itself as the delegate. In Figure 3, we assign <em>self</em> to <em>self.viewModel.homeViewModelDelegate</em> in the init function.</p><p>We then create an extension of ViewController to conform to <em>HomeViewModelDelegate </em>protocol, and implement the required methods that come with the protocol. In the example below, from the method <em>didFinishFetchingData(_ message: String)</em>, we receive the message passed from ViewModel. We can also update the user interface (UI) upon receiving the data. This completes the whole delegation structure.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7de45cb2403062d30c76c456a170c231/href">https://medium.com/media/7de45cb2403062d30c76c456a170c231/href</a></iframe><h4><strong>Data Binding: Closure</strong></h4><p>Implementing closure callbacks is relatively more flexible compared to delegations. Closures in Swift are blocks of codes that can be stored and passed as arguments to functions, frequently used in Apple SDKs (Software Development Kits) for event handling. Using closures as completion handlers is prevalent in many APIs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dcfb98b2ff3baa97e988d4f6cefe1236/href">https://medium.com/media/dcfb98b2ff3baa97e988d4f6cefe1236/href</a></iframe><p><strong>Implementation requires the following steps:</strong></p><ol><li>In ViewModel, declare a property that is closure type. In Figure 4, we declare a closure named <em>didFinishFetchingData</em> in ViewModel.</li><li>Call this property when you need to update ViewController. In Figure 4, we call this property in <em>fetchData</em> once the data is ready to be passed to ViewController.</li><li>In ViewController, access the property and assign a block of code to it, which will be executed once ViewModel triggers the callback method.</li></ol><p>In Figure 5, in ViewDidLoad method, we assign a block of code to <em>didFinishFetchingData</em> where we obtain the message from ViewModel. We can do more tasks once the data is back, for example, updating the UI. This wraps up the implementation of closure callback.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75335cae87285bd1ca28abe939aeab61/href">https://medium.com/media/75335cae87285bd1ca28abe939aeab61/href</a></iframe><h4><strong>Closure VS Delegation: Which To Go For</strong></h4><p>When selecting the right mechanism for data binding, apart from matching the coding style with your team member, another key point is to check how often ViewModel needs to update ViewController.</p><p>If only one or two callbacks are required, I would go for closure. However, if ViewModel needs to update ViewController several times, I would go for delegation.</p><p>The reason is simple: having several callback closures scattered in the codebase decreases readability and control over the code. It is also costly to implement delegation, and it wouldn’t be cost-efficient if we were to use it for just one callback.</p><h4><strong>How We Leveraged MVVM In Card+</strong></h4><p>At DBS, we adopt MVVM and flow coordinator pattern in our Card+ app, which contains the three essential parts of MVVM — ViewController, ViewModel, Model, and flow coordinator. To make our ViewModel less complex, we separate networking and routing from it. Take one simple app page for example, if there’s just one screen, it would usually comprise ViewController, ViewModel, Model, and Service. The file tree usually looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sqVrFM58dTbS7bgUTLrWTA.png" /><figcaption><em>Figure 6: The files tree for an app that has just one screen</em></figcaption></figure><p><strong>Coordinator — Routing With Rxflow</strong></p><p>RxFlow, a third-party navigation framework for iOS applications based on the Reactive Flow Coordinator pattern, manages all the screen navigations. We adopt RxFlow instead of the native Apple routing system as RxFlow increases dependency injection, testability, and UIViewController reusability. In addition, it is easy to manage navigation throughout the app as all the navigation cases are listed under one class. This involves four steps.</p><ol><li>Create an enum that inherits <em>Step</em>, where all screens are declared for navigation use. This enum covers all the possibilities we have for navigation. For example, if we have two screens in an app, declare an enum in this manner:</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c88a734cb31b5bdf602a79b1395f4297/href">https://medium.com/media/c88a734cb31b5bdf602a79b1395f4297/href</a></iframe><p>2. Add in <em>Flow</em>, which defines a navigation area within the app. We also create a class that inherits <em>Flow,</em> where we initialise all ViewModels and ViewControllers within one method</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f115501d6409f0b8a127b6f98f54289/href">https://medium.com/media/6f115501d6409f0b8a127b6f98f54289/href</a></iframe><p>3. Instruct ViewModel to inherit <em>Stepper</em> and declare a property to store <em>PublishRelay</em>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/979b517047a561cb401dc01fdfe99fb3/href">https://medium.com/media/979b517047a561cb401dc01fdfe99fb3/href</a></iframe><p>4. Finally, when there is a need for navigation, we trigger our steps in the following manner:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d08f16b5540e183a31270298ea78618/href">https://medium.com/media/5d08f16b5540e183a31270298ea78618/href</a></iframe><h4><strong>Conclusion</strong></h4><p>In Card+ app, we adopt MVVM with Flow Coordinator method to achieve dependency injection and increase code testability as well. With this architecture pattern, we can avoid the massive ViewController, which repeatedly exists in code using the Model-View-ViewController(MVVC) architecture pattern. Using a suitable architecture pattern also improves application maintenance.</p><p><em>Hsin Tzu Yen is an iOS developer at DBS Taiwan and works on Card+.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ef97657f180" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LeetCode #15, 3Sum— Swift solution]]></title>
            <link>https://medium.com/@hsintzuyen/leetcode-15-3sum-swift-solution-8ff75b6aa798?source=rss-e564bd53871f------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ff75b6aa798</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[leetcode-medium]]></category>
            <category><![CDATA[leetcode-solution]]></category>
            <category><![CDATA[swfit]]></category>
            <dc:creator><![CDATA[Hsin]]></dc:creator>
            <pubDate>Sun, 01 May 2022 13:55:50 GMT</pubDate>
            <atom:updated>2022-05-01T13:55:50.444Z</atom:updated>
            <content:encoded><![CDATA[<p>💡<a href="https://leetcode.com/problems/3sum/"><em>LeetCode Link</em></a></p><h3>Description:</h3><p>Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.</p><p>Notice that the solution set must <strong>not contain duplicate triplets</strong>.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> nums = [-1,0,1,2,-1,-4]<br><strong>Output:</strong> [[-1,-1,2],[-1,0,1]]</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> nums = []<br><strong>Output:</strong> []</pre><p><strong>Example 3:</strong></p><pre><strong>Input:</strong> nums = [0]<br><strong>Output:</strong> []</pre><p><strong>Constraints:</strong></p><ul><li>0 &lt;= nums.length &lt;= 3000</li><li>-105 &lt;= nums[i] &lt;= 105</li></ul><h3>Concept:</h3><p>If you never solved Two Sum II, I would recommend to start with <a href="https://medium.com/@hsintzuyen/leetcode-167-two-sum-ii-input-array-is-sorted-swift-solution-e22c19e98ba"><strong><em>this</em></strong></a> first.</p><p>Now if you already know the concept of Two Sum II, then 3 Sum is nothing more than a two sum adding up the other number.</p><p>Remember we need to skip the duplicate triplet, so we need to check whether the number is the same as the previous one.</p><h3>Code:</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f5e970b88c7c1b36e2653737e98685c/href">https://medium.com/media/0f5e970b88c7c1b36e2653737e98685c/href</a></iframe><p>That’s it for now, see ya next time!</p><p><em>If you think this is helpful, give me some claps 😌 any comments are welcome!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ff75b6aa798" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LeetCode #167 Two Sum II — Input Array Is Sorted— Swift solution]]></title>
            <link>https://medium.com/@hsintzuyen/leetcode-167-two-sum-ii-input-array-is-sorted-swift-solution-e22c19e98ba?source=rss-e564bd53871f------2</link>
            <guid isPermaLink="false">https://medium.com/p/e22c19e98ba</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[leetcode-medium]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[leetcode-solution]]></category>
            <dc:creator><![CDATA[Hsin]]></dc:creator>
            <pubDate>Sun, 01 May 2022 13:32:00 GMT</pubDate>
            <atom:updated>2022-05-01T13:32:00.054Z</atom:updated>
            <content:encoded><![CDATA[<h3>LeetCode #167 Two Sum II — Input Array Is Sorted— Swift solution</h3><p>💡<a href="https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/"><em>LeetCode Link</em></a></p><h3>Description:</h3><p>Given a <strong>1-indexed</strong> array of integers numbers that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 &lt;= index1 &lt; index2 &lt;= numbers.length.</p><p>Return<em> the indices of the two numbers, </em>index1<em> and </em>index2<em>, </em><strong><em>added by one</em></strong><em> as an integer array </em>[index1, index2]<em> of length 2.</em></p><p>The tests are generated such that there is <strong>exactly one solution</strong>. You <strong>may not</strong> use the same element twice.</p><p>Your solution must use only constant extra space.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> numbers = [2,7,11,15], target = 9<br><strong>Output:</strong> [1,2]<br><strong>Explanation:</strong> The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> numbers = [2,3,4], target = 6<br><strong>Output:</strong> [1,3]<br><strong>Explanation:</strong> The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].</pre><p><strong>Example 3:</strong></p><pre><strong>Input:</strong> numbers = [-1,0], target = -1<br><strong>Output:</strong> [1,2]<br><strong>Explanation:</strong> The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].</pre><p><strong>Constraints:</strong></p><ul><li>2 &lt;= numbers.length &lt;= 3 * 104</li><li>-1000 &lt;= numbers[i] &lt;= 1000</li><li>numbers is sorted in <strong>non-decreasing order</strong>.</li><li>-1000 &lt;= target &lt;= 1000</li><li>The tests are generated such that there is <strong>exactly one solution</strong>.</li></ul><h3>Step by step:</h3><blockquote>This solution uses a technique called “two-pointer”. <a href="https://leetcode.com/discuss/study-guide/1688903/Solved-all-two-pointers-problems-in-100-days."><strong>Here</strong></a> sums up some patterns of two pointers and gives you an idea of what two pointers is.</blockquote><p>Basically, we can set <strong>a left pointer at the beginning</strong> and <strong>a right one at the end</strong>, which allow us to track the indices of the numbers array.</p><p>Because the given array is sorted and ascending, we then can add up the two numbers, which result in 3 types of scenarios.</p><ol><li>sum &gt; target: shift the right pointer to the left</li><li>sum &lt; target: shift the left pointer to the right</li><li>sum == target: <strong>BINGO</strong>, return the answer</li></ol><h3>Code:</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3559fbbf190483c57eecec383f4dc723/href">https://medium.com/media/3559fbbf190483c57eecec383f4dc723/href</a></iframe><p>That’s it for now, see ya next time!</p><p><em>If you think this is helpful, give me some claps 😌 any comments are welcome!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e22c19e98ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LeetCode #1 Two Sum — Swift solution]]></title>
            <link>https://medium.com/@hsintzuyen/leetcode-1-two-sum-swift-solution-d375a9d13405?source=rss-e564bd53871f------2</link>
            <guid isPermaLink="false">https://medium.com/p/d375a9d13405</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[leetcode-easy]]></category>
            <category><![CDATA[two-sum]]></category>
            <dc:creator><![CDATA[Hsin]]></dc:creator>
            <pubDate>Sat, 09 Apr 2022 10:28:15 GMT</pubDate>
            <atom:updated>2022-04-24T07:32:32.006Z</atom:updated>
            <content:encoded><![CDATA[<h3>LeetCode #1 Two Sum — Swift solution</h3><p>💡<a href="https://leetcode.com/problems/two-sum/"><em>LeetCode Link</em></a></p><h3>Description:</h3><p>Given an array of integers nums and an integer target, return <em>indices of the two numbers such that they add up to </em><em>target</em>.</p><p>You may assume that each input would have <strong><em>exactly</em> one solution</strong>, and you may not use the <em>same</em> element twice.</p><p>You can return the answer in any order.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> nums = [2,7,11,15], target = 9<br><strong>Output:</strong> [0,1]<br><strong>Explanation:</strong> Because nums[0] + nums[1] == 9, we return [0, 1].</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> nums = [3,2,4], target = 6<br><strong>Output:</strong> [1,2]</pre><p><strong>Example 3:</strong></p><pre><strong>Input:</strong> nums = [3,3], target = 6<br><strong>Output:</strong> [0,1]</pre><p><strong>Constraints:</strong></p><ul><li>2 &lt;= nums.length &lt;= 104</li><li>-109 &lt;= nums[i] &lt;= 109</li><li>-109 &lt;= target &lt;= 109</li><li><strong>Only one valid answer exists.</strong></li></ul><h3>Step by step:</h3><p>Starting from the first number of the given array, add it to the second one.</p><p>-&gt; If the outcome matches the target, break and return the result.</p><p>-&gt; If not, keep adding the first number to the third one</p><p>If the first number fails, the outerLoop continues the computation with the second number, and so on.</p><h3>For loop solution :</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc5dd4b1ae223273cf3b8f067b8a9fbe/href">https://medium.com/media/bc5dd4b1ae223273cf3b8f067b8a9fbe/href</a></iframe><p><strong>Runtime</strong>: 88 ms, faster than 32.66% of Swift online submissions for Two Sum. <strong>Memory Usage</strong>: 14.4 MB, less than 53.46% of Swift online submissions for Two Sum. (Def needs some improvement 😛)</p><h3>Dictionary solution:</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc210ad67ac648b9fc450491d215a819/href">https://medium.com/media/fc210ad67ac648b9fc450491d215a819/href</a></iframe><p><strong>Runtime</strong>: 73 ms, faster than 45.81% of Swift online submissions for Two Sum. <strong>Memory Usage</strong>: 14.7 MB, less than 11.23% of Swift online submissions for Two Sum.</p><p>That’s it for now, see ya next time!</p><p><em>If you think this is helpful, give me some claps 😌 any comments are welcome!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d375a9d13405" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LeetCode #7 Reverse Integer — Swift solution]]></title>
            <link>https://medium.com/@hsintzuyen/leetcode-7-reverse-integer-swift-solution-44a0b8c76aec?source=rss-e564bd53871f------2</link>
            <guid isPermaLink="false">https://medium.com/p/44a0b8c76aec</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Hsin]]></dc:creator>
            <pubDate>Sat, 26 Mar 2022 05:10:44 GMT</pubDate>
            <atom:updated>2022-03-26T05:21:02.776Z</atom:updated>
            <content:encoded><![CDATA[<p>💡<a href="https://leetcode.com/problems/reverse-integer/"><em>LeetCode Link</em></a></p><h3><strong>Description:</strong></h3><p>Given a signed 32-bit integer x, return x<em> with its digits reversed</em>. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.</p><p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p><ul><li><strong>Constraints: </strong>-231 &lt;= x &lt;= 231 - 1</li></ul><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> x = 123<br><strong>Output:</strong> 321</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> x = -123<br><strong>Output:</strong> -321</pre><p><strong>Example 3:</strong></p><pre><strong>Input:</strong> x = 120<br><strong>Output:</strong> 21</pre><h3>Step by step:</h3><p><strong>Identify whether the number is negative -&gt; Get the last digit of the number -&gt; Remove the last digit from the number -&gt; Shift the digit sequence -&gt; Make the output negative if it was-&gt; Identify whether the outcome exceeds the 32-bit integer range</strong></p><ol><li><strong>Identify whether the number is negative</strong></li></ol><p>Simple enough, use an <strong>if</strong> statement to distinguish.</p><p>If the number IS negative, we make the negative variable to be -1.</p><pre>var input = x</pre><pre>var output = 0</pre><pre>var negative = 1<br></pre><pre>if input &lt; 0 {</pre><pre>negative = -1</pre><pre>input = input * negative</pre><pre>}</pre><p><strong>From steps 2~4, we will run a while loop with the below condition:</strong></p><pre>while input &gt; 0 {}</pre><p>2. <strong>Get the last digit of the number</strong></p><p>Using remainder operator %, we get the last digit.</p><pre>let lastDigit = input % 10</pre><p>3. <strong>Remove the last digit from the number</strong></p><p>As input is Integer type, if we divide it by 10 and assign it back to itself, it gets truncated, therefore, the last digit is removed.</p><pre>input = input / 10</pre><p>4. <strong>Shift the digit sequence</strong></p><p>Take 123 as an example here:</p><p>At the first run, the output will be the last digit itself. [outcome: 3]</p><p>At the second run, now we shift the last digit to the first place. [outcome: 30 + 2 = 32]</p><p>and so on…</p><pre>output = (output * 10) + lastDigit</pre><p>5.<strong> Make the output negative if it was</strong></p><pre>output = output * negative</pre><p>6. <strong>Identify whether the outcome exceeds the 32-bit integer range</strong></p><p>Check if the result meets the question constraint: <strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p><pre>guard output &lt; Int32.max &amp;&amp; output &gt; Int32.min else {</pre><pre>return 0</pre><pre>}</pre><h3>Solution:</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53b7be0cd211a073a6086dfd8e7bf8fa/href">https://medium.com/media/53b7be0cd211a073a6086dfd8e7bf8fa/href</a></iframe><p>Result: <strong>Runtime:</strong> 0 ms, faster than 100.00% of Swift online submissions for Reverse Integer. <strong>Memory Usage:</strong> 13.9 MB, less than 40.78% of Swift online submissions for Reverse Integer.</p><p>You deserve a happy dance at the end!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F5xaOcLGvzHxDKjufnLW%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F5xaOcLGvzHxDKjufnLW%2Fgiphy.gif&amp;image=https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExeHYwbDNmZzJ0OWUwbXozMzJqNmpiOHNsbnk0cHJxZWp4NGJwZHZjMyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw%2F5xaOcLGvzHxDKjufnLW%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="298" frameborder="0" scrolling="no"><a href="https://medium.com/media/25c266506f609b10da553fb64572b5a1/href">https://medium.com/media/25c266506f609b10da553fb64572b5a1/href</a></iframe><p>That’s it for now, see ya next time!</p><p><em>If you think this is helpful, give me some claps 😌 any comments are welcome!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=44a0b8c76aec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Face ID & Touch ID — Customized cancel button title and fallback button title in Swift, iOS]]></title>
            <link>https://medium.com/@hsintzuyen/face-id-touch-id-customized-cancel-button-title-and-fallback-button-title-in-swift-ios-c528551c9ef3?source=rss-e564bd53871f------2</link>
            <guid isPermaLink="false">https://medium.com/p/c528551c9ef3</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[biometric-authentication]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[swift-programming]]></category>
            <dc:creator><![CDATA[Hsin]]></dc:creator>
            <pubDate>Mon, 14 Mar 2022 13:43:41 GMT</pubDate>
            <atom:updated>2022-03-14T13:43:41.654Z</atom:updated>
            <content:encoded><![CDATA[<h3>Face ID &amp; Touch ID — Customized cancel button title and fallback button title in Swift, iOS</h3><p><em>I had trouble finding this kind of info online back when I fixed biometric authentication functionalities, thus, here’s my note. :)</em></p><h3>For starters</h3><p>First thing first, when implementing Face ID &amp; Touch ID functionalities, it is required to add the <strong>key</strong> “<strong>Privacy — Face ID Usage Description</strong>” to <strong>Info.plist</strong>.</p><p>You can customize the description of the reason by adding a string to the <strong>value</strong>. Here I simply put “Use Face ID to login” for instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QE77kOmKDn9v5bw5_BeyVA.png" /><figcaption>Info.plist</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/891/1*-M8SJO0TDagaJQEJM5ykCg.png" /><figcaption>Customized reason</figcaption></figure><h3>Codewise</h3><p>Now let’s dive into the titles of the popup buttons when users proceed with biometric authentication.</p><p>Here’s some sample code of <strong>LAContext</strong>. I will demonstrate the outcomes of these codes individually below. Note that we can customize these descriptions by assigning the value to the following properties.</p><blockquote>Cancel button title: <strong>localizedCancelTitle</strong></blockquote><blockquote>Fall back button title: <strong>localizedFallbackTitle</strong></blockquote><p>Also, the argument “<strong>localizedReason</strong>” of <em>evaluatePolicy</em> function is also a value we can control.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6bee9d6bfdc300fe41800af36fe12f4c/href">https://medium.com/media/6bee9d6bfdc300fe41800af36fe12f4c/href</a></iframe><p>It’s always fuzzy to only see the code itself. Below are the simulator images from both Touch ID &amp; Touch ID.</p><h3>Touch ID</h3><p>First attempt:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*GlXxTm62aNY9M0wOKrEqfw.png" /></figure><p>If first attempt fails -&gt; second attempt &amp; possible third attempt:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*-JoLvcxZFPZjUyMyiwwzTw.png" /></figure><h3>Face ID</h3><p>First attempt:</p><p>*simulator image varies from the real device</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*XU8ox1CggqskxdklVK00dA.png" /></figure><p>If first attempt fails -&gt; second attempt:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/474/1*qYiT2UrYxUd-SwWmGT1J4Q.png" /></figure><p>If the second attempt fails -&gt; third attempt:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/474/1*uPhVxkMjs4RiebF8n6_9Pg.png" /></figure><p>Alright, this wraps up the note! I hope you take away some useful info from reading this 🤓</p><h3>BONUS: Test biometric authentication on simulators</h3><p>For those of you who wonder how to use simulator testing biometric authentication. Here’s the trick.</p><p><strong>Simulator -&gt; Features -&gt; Face ID -&gt; Enrolled</strong></p><p>After ticking “Enrolled”, it’s set to Face ID enabled.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6xcDk0CBoxkCYMANCSa-bA.png" /></figure><p>That’s it for now, see ya next time!</p><p><em>If you think this is helpful, give me some claps 😌</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c528551c9ef3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Static and Class Modifiers in Swift, iOS]]></title>
            <link>https://medium.com/@hsintzuyen/static-and-class-modifiers-in-swift-ios-1bf490aae38f?source=rss-e564bd53871f------2</link>
            <guid isPermaLink="false">https://medium.com/p/1bf490aae38f</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Hsin]]></dc:creator>
            <pubDate>Wed, 23 Feb 2022 13:59:13 GMT</pubDate>
            <atom:updated>2022-09-25T02:32:52.474Z</atom:updated>
            <content:encoded><![CDATA[<p>This note was actually inspired by an interview question on <a href="https://www.raywenderlich.com/">raywenderlich.com</a>.</p><p>A question from <a href="https://www.raywenderlich.com/762435-swift-interview-questions-and-answers#toc-anchor-019"><strong>here</strong></a>, <strong>Intermediate Verbal Questions, Question #2.</strong></p><p>I think it would be a great idea if you could go check the interview question and test if you know the answer first.</p><h3>Now let’s get started!</h3><blockquote>staticand class modifiers can be used on a property or a function to make a <strong>type property</strong> or a <strong>type function</strong>.</blockquote><p>If you ask…what are type property and type function?</p><blockquote>Type properties and type functions belong to the <strong>type itself</strong>, not to the instance of the type.</blockquote><blockquote>There will only be one copy of type properties or type functions.</blockquote><p>Then, what is the difference between static and class ?</p><blockquote>First of all, staticcan be used within <strong>classes AND structs</strong>.</blockquote><blockquote>However, class can ONLY be used within<strong> classes</strong>, which is easy to remember, class modifier only for classes.</blockquote><p>Struct example explained in code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41642fe58bdc0d68a40ce8c45dd4f85b/href">https://medium.com/media/41642fe58bdc0d68a40ce8c45dd4f85b/href</a></iframe><p>Class example explained in code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46157db63cad63ed92d0690e81fde92b/href">https://medium.com/media/46157db63cad63ed92d0690e81fde92b/href</a></iframe><p>What’s interesting and noteworthy in the above code is the error in the class example “<strong><em>Class stored properties not supported in classes</em></strong>”.</p><blockquote>This means when you are making type properties in classes, <em>class only support</em><strong><em> computed properties</em></strong><em>, not stored properties.</em></blockquote><h3>The second difference is <strong>Overridability(</strong>inheritance<strong>)</strong>.</h3><blockquote>static does NOT allow override, whereas class does.</blockquote><p>let’s see a code example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/917a01cfbb032491e66afa2fc454e684/href">https://medium.com/media/917a01cfbb032491e66afa2fc454e684/href</a></iframe><p>Therefore, use the class modifier when the function needs to be overridden.</p><p>That’s it for now, see ya next time!</p><p><em>If you think this is helpful, give me some claps 😌</em></p><p>Reference: <a href="https://docs.swift.org/swift-book/LanguageGuide/Properties.html">https://docs.swift.org/swift-book/LanguageGuide/Properties.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1bf490aae38f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Access control in Swift, iOS]]></title>
            <link>https://medium.com/@hsintzuyen/access-control-in-swift-ios-d7a03b8ad322?source=rss-e564bd53871f------2</link>
            <guid isPermaLink="false">https://medium.com/p/d7a03b8ad322</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios-app-developers]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Hsin]]></dc:creator>
            <pubDate>Sun, 20 Feb 2022 09:27:31 GMT</pubDate>
            <atom:updated>2022-02-21T15:50:06.150Z</atom:updated>
            <content:encoded><![CDATA[<p>As a beginner, I was not fully aware of how I should use “Access Control” while coding. Along the way, I found I missed out on one critical point, which is the concept of <strong>Source files and Modules</strong> in Swift.</p><h4>Definition of Modules</h4><p>According to <em>Apple documentation</em>: <em>“A module is a single unit of code distribution — a framework or application that’s built and shipped as a single unit and that can be imported by another module with Swift’s </em><em>import keyword.”</em></p><p>…… okay I admit I was a bit lost after reading this paragraph.</p><p>BUT, basically, a module is what you see in the <strong>TARGETS</strong> section:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*wRoJR_hk27OVqY0gDvPSaQ.png" /></figure><p>As you can see in the above image, there are 2 targets:</p><ol><li>PersonalityQuiz-Apple guided <strong>app</strong></li><li>Sample<strong>Framework</strong></li></ol><blockquote>Each build target, no matter an <strong>App</strong>, or a <strong>Framework</strong>, to Swift compiler, is an individual <strong><em>Module</em></strong>.</blockquote><h4>Definition of Source Files:</h4><p>According to <a href="https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html"><em>Apple documentation</em></a>: <em>“A source file is a single Swift source code file within a module (in effect, a single file within an app or framework). Although it’s common to define individual types in separate source files, a single source file can contain definitions for multiple types, functions, and so on.”</em></p><blockquote>Simply put, you can think of <strong>a source file is a .swift file</strong>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*NB9adZL-S0eizy30qSrnaw.png" /></figure><p>To put them in one image:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yLDys9_9zuvOS5KW5mJymA.png" /><figcaption>source files and modules</figcaption></figure><p>Now that we have the Source files and Modules concept in mind, let’s move on to the 5 levels of Swift Access Control.</p><h4>5 Levels of Access Control, least restrictive to most restrictive, top to bottom:</h4><ol><li>Open</li><li>Public</li><li>Internal (<em>default</em>)</li><li>File-private</li><li>Private</li></ol><p>Let’s discuss each one of them.</p><h4>Open and Public:</h4><p><strong>Open and Public both make the entities can be accessed from ANY source files, no matter from the same or different modules.</strong> If you access the desired entity from different module, you simply import the module in the source file, and you would have the access.</p><blockquote>The key difference is that<strong> Open</strong> only applies to <strong>classes and class members. </strong>On top of that, <strong>Open</strong> makes the entity able to be <strong>subclassed and overridden</strong>.</blockquote><p>Therefore, in most cases, I would declare an entity to be <strong>Public</strong>, because I would not want others to change the entity easily.</p><h4>Internal:</h4><blockquote>By default, every entity without specifying access level is Internal.</blockquote><p>By this I mean, if you declare a constant and leave it without any access level in front of the constant, it is marked <strong>Internal</strong> by Swift compiler.</p><p>At this level, you can access the entity from ANY source files in the <strong>SAME MODULE</strong>.</p><h4>File-private:</h4><p>With this keyword, you can only access the entity in <strong>the SAME source file</strong>.</p><h4>Private:</h4><p>You can only access from the enclosing<strong> declaration</strong>, and to <strong>extensions</strong> of that declaration that are in the same file.</p><p>For example, there is one class Humanand there’s a private var age: Int , you could only access age property within class Human and its extensions.</p><p>Alright, this wraps up the basic ideas of Access Control in Swift.</p><p>See you next time! 🙌🏽</p><p><em>If you think this is helpful, give me some claps 😌</em></p><p>Reference: <a href="https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html">https://docs.swift.org/swift-book/LanguageGuide/AccessControl.h</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d7a03b8ad322" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>