<?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 Arias on Medium]]></title>
        <description><![CDATA[Stories by Luis Arias on Medium]]></description>
        <link>https://medium.com/@lugearma?source=rss-7520f759305f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GdF1EQ-tK0FsWY-e-NjPLA.png</url>
            <title>Stories by Luis Arias on Medium</title>
            <link>https://medium.com/@lugearma?source=rss-7520f759305f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 04:11:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lugearma/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[Reusable UIAlertController — Protocol Oriented Programming perspective]]></title>
            <link>https://medium.com/@lugearma/reusable-uialertcontroller-protocol-oriented-programming-perspective-3264db0cd8bf?source=rss-7520f759305f------2</link>
            <guid isPermaLink="false">https://medium.com/p/3264db0cd8bf</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[protocol-oriented]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Luis Arias]]></dc:creator>
            <pubDate>Tue, 05 Feb 2019 16:14:59 GMT</pubDate>
            <atom:updated>2019-02-05T16:14:59.071Z</atom:updated>
            <content:encoded><![CDATA[<h3>Reusable UIAlertController — Protocol Oriented Programming perspective</h3><p>Sometimes we have to present alerts in many places in our apps, here I’m gonna present a way I found to tackle this thing reusing code.</p><p>I’m gonna use <strong>Protocol Oriented Programming (POP)</strong> which is a great Swift paradigm that allows us make things from a different perspective than Object Oriented Programming.</p><p>First of all, create a new <em>Swift</em> file. I’ll call it: <em>AlertPresentable.swift </em>(I’m too bad naming things 😓)</p><p>As we’re are using POP, the first thing we’re gonna make is create a <em>protocol </em>like this:</p><pre><strong>protocol</strong> AlertPresentable {</pre><pre>}</pre><p>Then a good practice is restrict which class/struct or any kind of entity can implement the protocol. As you may know alerts only can be presented by <em>UIViewController </em>subclasses (remember that <em>UIAlertController</em> is also a <em>UIViewController </em>subclass<em> </em>🤓).</p><p>Having said that, let’s use that concept and limit the “implementators” to only <em>UIViewController</em> subclasses.</p><pre><strong>protocol</strong> AlertPresentable <strong>where</strong> Self: UIViewController {</pre><pre>}</pre><p>This part of code is very explicit, it’s saying something like: “<strong>where</strong> the guy(<strong>Self</strong>) who is gonna implement to me must be a subclass of <strong>UIViewController” 😌</strong></p><p>To verify this you should try to implement the protocol from a class that is not a subclass of <em>UIViewController</em> and you’re going to get a compile error.</p><p>Perfect, let’s talk(very briefly) about how an alert is composed and based on that define a couple of entities that we need to make this work.</p><p>An <strong>alert </strong>(<a href="https://developer.apple.com/documentation/uikit/uialertcontroller">UIAlertController</a>) is composed for some properties like <em>title, message, preferredStyle, </em>etc… and also an alert has associated <strong>actions </strong>(<a href="https://developer.apple.com/documentation/uikit/uialertaction">UIAlertAction</a>), which are composed by properties like <em>title, style, handler,</em> etc…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8c3TaTASrzfRYXDZagGmTw.png" /></figure><p>Having said that, let’s “split” these two concepts in two entities. The first entity will be called <em>AlertComponents, </em>this will be a value semantics type (in this case struct). This entity will represent the data we need to make our alert work.</p><pre><strong>struct</strong> AlertComponents {</pre><pre><strong>    var</strong> title: String?<br>    <strong>var</strong> message: String?<br>    <strong>var</strong> actions: [UIAlertAction]<br>    <strong>var</strong> completion: (() -&gt; Void)?</pre><pre><strong>    init</strong>(title: String?, message: String? = <strong>nil</strong>, actions: [AlertActionComponent], completion: (() -&gt; Void)? = <strong>nil</strong>) {<br>        <strong>self</strong>.title = title <br>        <strong>self</strong>.message = message<br>        <strong>self</strong>.completion = completion<br>        <strong>self</strong>.actions = actions.map {<br>            UIAlertAction(title: $0.title, style: $0.style, handler: $0.handler)<br>        }<br>    }<br>}</pre><p>Here, we have 4 properties:</p><ul><li><strong>title</strong>: optional string (an alert can or cannot have a title) that will represent the alert’s title.</li><li><strong>message</strong>: optional string which represents the message.</li><li><strong>actions</strong>: array of <em>UIAlertAction</em> objects that will be associated to the alert.</li><li><strong>completion</strong>: optional function that will be executed once the alert has been presented.</li></ul><p>An alert at least must to have one action let’s create another entity (struct again) called <em>AlertActionComponent</em> which will represent the action or actions we want to associate to the alert.</p><pre><strong>struct</strong> AlertActionComponent {<br>    <br>    <strong>var</strong> title: String<br>    <strong>var</strong> style: UIAlertAction.Style<br>    <strong>var</strong> handler: ((UIAlertAction) -&gt; Void)?</pre><pre><strong>    init</strong>(title: String, style: UIAlertAction.Style = .default, handler: ((UIAlertAction) -&gt; Void)?) {<br>        <strong>self</strong>.title = title<br>        <strong>self</strong>.style = style<br>        <strong>self</strong>.handler = handler<br>    }<br>}</pre><ul><li><strong>title</strong>: string which represent the action’s title</li><li><strong>style</strong>: the action’s style, take a look <a href="https://developer.apple.com/documentation/uikit/uialertaction/style">here</a>.</li><li><strong>handler</strong>: a function that will be executed once the user taps that action (if value is nil it will only close the alert).</li></ul><p>Well done, after all this let’s go back to our protocol.</p><p>Now let’s define two properties in our protocol.</p><pre><strong>protocol</strong> AlertPresentable <strong>where</strong> Self: UIViewController {<br>    <strong>var</strong> alertStyle: UIAlertController.Style { <strong>get</strong> }<br>    <strong>var</strong> alertComponents: AlertComponents { <strong>get</strong> }<br>}</pre><ul><li><strong>alertStyle</strong>: the alert’s style, take a look <a href="https://developer.apple.com/documentation/uikit/uialertcontroller/style">here</a>.</li><li><strong>alertComponents</strong>: a value that will represent the components that we’re gonna use to create our alert.</li></ul><p>Now let’s extend our protocol.</p><pre><strong>extension</strong> AlertPresentable {</pre><pre>}</pre><p>Cool, when we extend a protocol we can do many things like: create new vars (computed ones), new functions (with implementation), give to our vars and functions we already defined in our protocol a default implementation, etc…</p><p>Let’s make use of this cool properties unlocked once we extended the protocol.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*q9U0DxOquINwT6UNpcxM2g.jpeg" /></figure><p>Let’s create a function and some computed properties that will help to present the alert.</p><pre><strong>extension</strong> AlertPresentable {<br>    <strong>private</strong> <strong>var</strong> alertTitle: String? {<br>        <strong>return</strong> alertComponents.title<br>    }</pre><pre><strong>    private</strong> <strong>var</strong> message: String? {<br>        <strong>return</strong> alertComponents.message<br>    }</pre><pre><strong>    private</strong> <strong>var</strong> actions: [UIAlertAction] {<br>        <strong>return</strong> alertComponents.actions<br>    }</pre><pre><strong>    var</strong> alertStyle: UIAlertController.Style {<br>        <strong>return</strong> .alert<br>    }</pre><pre><strong>    private</strong> <strong>var</strong> completion: (() -&gt; Void)? {<br>        <strong>return</strong> alertComponents.completion<br>    }</pre><pre><strong>    func</strong> presentAlert() {<br>        <strong>let</strong> alert = UIAlertController(title: alertTitle, message: message, preferredStyle: alertStyle)<br>        actions.forEach { alert.addAction($0) }<br>        present(alert, animated: <strong>true</strong>, completion: completion)<br>    }</pre><pre>}</pre><p>Ok, here some important points: we created some private computed properties to get our alert components values, as you can see the only property that is not <em>private</em> is <em>alertStyle, </em>we’re giving to it a default value, when we implement the protocol we’re not required to implement that var because it has a default value, but if you want a different style you have to change that in your implementation.</p><p>Finally we created a function which basically creates the alert, assign the actions and present it.</p><p>Remember the “where Self: UIViewController” ?? this allow us to call <em>present(_:animated:completion:)</em> function in our protocol extension because our protocol already knows that the guys is implementing is <em>UIViewController</em> type !!! (Love you Swift/POP 🥰)</p><p>We’re about to finish, we only have to use our protocol.<br>First let’s conform our protocol, here you have to set all the properties that your alert will use.</p><pre><strong>extension</strong> HomeViewController: AlertPresentable {</pre><pre><strong>    var</strong> alertComponents: AlertComponents {<br>        <strong>let</strong> action = AlertActionComponent(title: &quot;Ok&quot;, handler: {<strong>_</strong> <strong>in</strong> print(&quot;My task :)&quot;)})<br>        <strong>let</strong> alertComponents = AlertComponents(title: &quot;Alert&quot;, message: &quot;Not a simple one, is a POP one.&quot;, actions: [action], completion: {<br>            print(&quot;Just presented&quot;)<br>    })</pre><pre><strong>        return</strong> alertComponents<br>    }</pre><pre>}</pre><p>and the last step, use it calling <em>presentAlert()</em> whenever you need.</p><pre><strong>override</strong> <strong>func</strong> viewDidAppear(<strong>_</strong> animated: Bool) {<br>    <strong>super</strong>.viewDidAppear(animated) <br>    presentAlert()<br>}</pre><p>Here’s a nice gist where you can see the entire implementation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96e39a5637485fedfa0d14ecaa93cd95/href">https://medium.com/media/96e39a5637485fedfa0d14ecaa93cd95/href</a></iframe><p>Finally if you find an error, an improvement or something you would like to share just leave a comment !!!!</p><p>Made with ❤️ by <a href="https://medium.com/u/7520f759305f">Luis Arias</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3264db0cd8bf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using enums properly to represent string values]]></title>
            <link>https://medium.com/@lugearma/using-enums-properly-to-represent-string-values-6173c05d5fc0?source=rss-7520f759305f------2</link>
            <guid isPermaLink="false">https://medium.com/p/6173c05d5fc0</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[app-development]]></category>
            <dc:creator><![CDATA[Luis Arias]]></dc:creator>
            <pubDate>Tue, 03 Oct 2017 03:43:55 GMT</pubDate>
            <atom:updated>2017-10-03T03:44:08.708Z</atom:updated>
            <content:encoded><![CDATA[<h4>Motivation</h4><p>The motivation of this post is to show how we can use enums properly to represent string values.</p><p>This is a great tool that you can use in your projects. 🍷</p><h4>Steps 👣</h4><p>First let’s create a struct that will represent the name’s state</p><pre>struct State {</pre><pre>let name: String</pre><pre>  init(name: String) {<br>     self.name = name<br>  }<br>}</pre><p>then let’s extend the struct conforming <a href="https://developer.apple.com/documentation/swift/expressiblebystringliteral"><em>ExpressibleByStringLiteral</em></a><em> </em>protocol, this will allow us to initialize our variable using a string literal.</p><pre>extension State: ExpressibleByStringLiteral {</pre><pre>  public init(stringLiteral value: StringLiteralType) {<br>    self.init(name: value)<br> }</pre><pre> public init(unicodeScalarLiteral value: String) {<br>    self.init(name: value)<br> }</pre><pre>  public init(extendedGraphemeClusterLiteral value: String) {<br>    self.init(name: value)<br>  }<br>}</pre><p>After this let’s make <em>Hashable</em> our struct to give it super powers 💪🏽</p><pre>extension State: Hashable {</pre><pre>  var hashValue: Int {<br>    return name.hashValue<br>  }</pre><pre>  static func == (lhs: State, rhs: State) -&gt; Bool {<br>    return lhs.name == rhs.name<br>  }<br>}</pre><p>to finalize this just create a protocol that will be implemented by any enum you want</p><pre>protocol StateRepresentable {<br>   var stateRepresentable: State { get }<br>}</pre><p>Ok, at this point we have finished the implementation, let’s see how we can use it. 😎</p><p>Suppose we have three different states to represent; <em>requested</em>, <em>pending</em>, <em>completed</em>.</p><pre>enum TicketState {<br>  case requested<br>  case pending<br>  case completed<br>}</pre><p>then extend our enum to implement our awesome protocol</p><pre>extension TicketState: StateRepresentable {<br>  var stateRepresentable: State {<br>    switch self {<br>      case .requested: return &quot;requested_service&quot;<br>      case .pending: return &quot;pending_service&quot;<br>      case .completed: return &quot;completed_service&quot;<br>    }<br>  }<br>}</pre><p>finally, use it 😉</p><pre>let requestedState: TicketState = .requested<br>let pendingState: TicketState = .pending</pre><pre>print(requestedState.stateRepresentable.name) // &quot;requested_service&quot;</pre><pre>if requestedState == pendingState {<br>  print(“✅”)<br>} else {<br>  print(“❌”)<br>}</pre><p>Here the complete implementation</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/35112b714197f5908d55fe7dd5f2a6e1/href">https://medium.com/media/35112b714197f5908d55fe7dd5f2a6e1/href</a></iframe><p>Thanks to read, if you have any comment or improvement please let me know in the comments.</p><p>Made with ❤️ by <a href="https://medium.com/u/7520f759305f">Luis Arias</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6173c05d5fc0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Converting Date String to Date Type with Swift]]></title>
            <link>https://medium.com/@lugearma/converting-date-string-to-date-type-with-swift-8b6b2f07d50?source=rss-7520f759305f------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b6b2f07d50</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[swift-programming]]></category>
            <dc:creator><![CDATA[Luis Arias]]></dc:creator>
            <pubDate>Fri, 29 Sep 2017 18:51:53 GMT</pubDate>
            <atom:updated>2018-10-06T01:11:06.171Z</atom:updated>
            <content:encoded><![CDATA[<h4>Why I wrote this 💩 ?</h4><p>This is my first post, my aim with this series of very small “tutorials” is to share with everyone out there my knowledge about Swift/iOS/watchOS development, as well as small tips that I found very useful in my projects. Also this’ll help me to remember what I learnt 😅.</p><p>Hope you find this chunks of code useful !!</p><p>So, let’s start !! 🤙🏽</p><h4>Motivation</h4><p>In one of my projects I had to check if a promo code was available when the user wanted to apply it, this because it had a expiration date, so I needed to perform something like this:</p><pre>if start_date &lt; current_date &lt; expiration_date ... {<br>    //Promo Code applies 👍🏽</pre><pre>} else {<br>    //Promo Code does not apply 👎🏽</pre><pre>}</pre><p>but the in the JSON I received that value was a String, and as you know use String value to compare dates is not a good approach, so I decided that the best way to do it is converting those Strings to Dates 😎</p><h4>Steps 👣</h4><p>Suppose we have this value</p><pre>let stringDate = &quot;2017-05-25&quot;</pre><p>first we have to create a <a href="https://developer.apple.com/documentation/foundation/dateformatter"><em>DateFormatter</em></a> instance, this object is basically the guy who is going to help us to perform the entire process</p><pre>let dateFormatter = DateFormatter()</pre><p>then we have to set the date format you have, in this case we have the <em>year</em> in first place, then the <em>month</em> and finally the <em>day. </em><a href="http://nsdateformatter.com/">Here a nice link where you can find your format.</a></p><pre>dateFormatter.dateFormat = “yyyy-MM-dd”</pre><p>And the final step is ask to our <em>dateFormatter</em> object create a new <em>Date?</em> instance base on our <em>stringDate</em></p><pre>let date = dateFormatter.date(from: stringDate)</pre><p>Nice, we got it !!! 💪🏽</p><p>Note: Be careful with this last step, that one can return <em>nil </em>if the <em>dateFormat</em> doesn’t match with the format of your <em>stringDate</em></p><p>Here a nice gist where you can see the entire implementation, also you can add this behaviour as String extension or something like that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da333b795e2ca94134aa86176b7f7df8/href">https://medium.com/media/da333b795e2ca94134aa86176b7f7df8/href</a></iframe><p>Thanks to read this little article and feel free to any recommendation or improvement, actually that’s another reason I wrote this.</p><p>Here’s the extension</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ba92c8c51b15ef3afc87812988a896f/href">https://medium.com/media/0ba92c8c51b15ef3afc87812988a896f/href</a></iframe><p>Made with ❤️ by <a href="https://medium.com/u/7520f759305f">Luis Arias</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b6b2f07d50" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>