# Design Patterns on iOS using Swift – Part 1/2

**URL:** <https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015>\
**Category:** 🎓 Courses & Articles\
**Created:** [August 7, 2017, 1:23pm UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015 "2017-08-07T13:23:47Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lorenzoboaro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lorenzoboaro/32/99103_2.png) [@lorenzoboaro](https://forums.kodeco.com/u/lorenzoboaro)\
**Post date:** [August 7, 2017, 1:23pm UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/1 "2017-08-07T13:23:48Z")

</div>

Learn about common design patterns when building iOS apps, and how you can apply these patterns in your own apps, in this 2-part tutorial.

* * *
 This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/477-design-patterns-on-ios-using-swift-part-1-2](https://www.raywenderlich.com/477-design-patterns-on-ios-using-swift-part-1-2)

---

<div class="post-metadata">

**Author:** ![batdido](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/batdido/32/81494_2.png) [@batdido](https://forums.kodeco.com/u/batdido)\
**Post date:** [August 7, 2017, 3:57pm UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/2 "2017-08-07T15:57:40Z")

</div>

Hey, Lorenzo,

the link that you have for the second part of the tutorial directs to the really old version of the tutorial from 2014. You may want to update that link cause you have the second part of the tutorial updated anyways 🙂 Nice work!

---

<div class="post-metadata">

**Author:** ![obusek](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/obusek/32/110125_2.png) [@obusek](https://forums.kodeco.com/u/obusek)\
**Post date:** [August 7, 2017, 7:34pm UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/3 "2017-08-07T19:34:32Z")

</div>

@batdido Thanks for the heads up, I just corrected the link.

---

<div class="post-metadata">

**Author:** ![ka\_dze](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ka_dze/32/81494_2.png) [@ka\_dze](https://forums.kodeco.com/u/ka_dze)\
**Post date:** [August 8, 2017, 7:15pm UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/4 "2017-08-08T19:15:34Z")

</div>

I’ve found more comprehensive explanation for Decorator.  
[https://medium.com/design-patterns-in-swift/design-patterns-in-swift-decorator-pattern-2026e7112869](https://medium.com/design-patterns-in-swift/design-patterns-in-swift-decorator-pattern-2026e7112869)  
I think that you describe it right, because the main aim is to extend the class without inheritance. But here is the implementation of GOF’s vision.

---

<div class="post-metadata">

**Author:** ![micazeve](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/micazeve/32/103912_2.png) [@micazeve](https://forums.kodeco.com/u/micazeve)\
**Post date:** [August 9, 2017, 9:47am UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/5 "2017-08-09T09:47:47Z")

</div>

Nice post about design patterns on Swift 🙂

I saw this great article about the one line singleton in Swift and wanted to share it here. :

> **[The Right Way To Write a Singleton — KrakenDev](https://krakendev.io/blog/the-right-way-to-write-a-singleton)**
>
> Due to the nature of a beta language, Swift didn't have all the right things it needed when it first started out. For instance, Singletons, over the course of the development of Swift from 1.0 to 1.2, gained several different implementations....

The author shows the evolution of the singleton, from the **Objective-C** singleton using `dispatch_once_t`, to the single line **Swift** static class variable.

The important point is the study of the backtrace in order to prove that a static class variable is initialized under the hood using `dispatch_once`, which haven’t been cleary stated in Apple documentation (just for global variables, but it’s not the same).

---

<div class="post-metadata">

**Author:** ![lorenzoboaro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lorenzoboaro/32/99103_2.png) [@lorenzoboaro](https://forums.kodeco.com/u/lorenzoboaro)\
**Post date:** [August 9, 2017, 10:23am UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/6 "2017-08-09T10:23:59Z")

</div>

@batdido Thank you so much for pointing out the wrong link!

---

<div class="post-metadata">

**Author:** ![lorenzoboaro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lorenzoboaro/32/99103_2.png) [@lorenzoboaro](https://forums.kodeco.com/u/lorenzoboaro)\
**Post date:** [August 9, 2017, 10:26am UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/7 "2017-08-09T10:26:13Z")

</div>

@ka_dze Thanks for sharing the decorator pattern by GoF!

---

<div class="post-metadata">

**Author:** ![lorenzoboaro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lorenzoboaro/32/99103_2.png) [@lorenzoboaro](https://forums.kodeco.com/u/lorenzoboaro)\
**Post date:** [August 9, 2017, 10:26am UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/8 "2017-08-09T10:26:50Z")

</div>

@micazeve Thanks for sharing this link! Very useful 😉

---

<div class="post-metadata">

**Author:** ![rommex73](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rommex73/32/81494_2.png) [@rommex73](https://forums.kodeco.com/u/rommex73)\
**Post date:** [August 19, 2017, 10:21am UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/9 "2017-08-19T10:21:27Z")

</div>

Excellent tutorial, thank you so much!! But the storyboard is opening only in Xcode 9 Beta… Arghhh)

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [August 19, 2017, 12:19pm UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/10 "2017-08-19T12:19:29Z")

</div>

Thank you for the heads up and pointing this one out - much appreciated. @lorenzoboaro Can you please take care of and handle this one when you actually get a chance? Thanks! :]

---

<div class="post-metadata">

**Author:** ![hemav](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@hemav](https://forums.kodeco.com/u/hemav)\
**Post date:** [August 20, 2017, 8:43am UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/11 "2017-08-20T08:43:45Z")

</div>

I am coming across issues with this tutorial. I downloaded XCode 9 beta and I am trying this out. but when I used Swift 4.0 the compiler is cribbing and when I set it Swift 3.1 it did not crib. But I also came across another issue on the line:

func addAlbum(\_ album: Album, at index: Int) {  
**persistencyManager.addAlbum(album, at: index)strong text**  
if isOnline {  
httpClient.postRequest(“/api/addAlbum”, body: album.description)  
}  
}  
This does not compile. I tried so many ways but has not accepted the same.  
These are the errors that it gives.  
 ![image](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/7/6/769b50aeb3cd70d8f44dafa8faeda03459bb6543.png)

Wonderful tutorial!! Please help me to get over these issues!! Thanks in advance

Hema

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [August 20, 2017, 2:13pm UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/12 "2017-08-20T14:13:14Z")

</div>

Thanks for the heads up @lorenzoboaro can you please help with this when you get a chance?

---

<div class="post-metadata">

**Author:** ![lorenzoboaro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lorenzoboaro/32/99103_2.png) [@lorenzoboaro](https://forums.kodeco.com/u/lorenzoboaro)\
**Post date:** [August 21, 2017, 12:15pm UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/13 "2017-08-21T12:15:34Z")

</div>

Hi @rommex73! Thanks for your feedback and sorry for the delay. The tutorial can be opened in Xcode 9 since I’m using the new Safe Area Layout Guide. That’s a new feature in iOS 11. If you want, you can still open Storyboards in Xcode 8 using the **Opens in** option under the **Interface Builder Document** in the **File inspector** tab. Let me know for anything!

---

<div class="post-metadata">

**Author:** ![lorenzoboaro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lorenzoboaro/32/99103_2.png) [@lorenzoboaro](https://forums.kodeco.com/u/lorenzoboaro)\
**Post date:** [August 21, 2017, 12:18pm UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/14 "2017-08-21T12:18:08Z")

</div>

Hi @hemav! Thanks for your feedback! Could you tell me what version of Xcode 9 beta version you are using? The project works fine for me. I’m using Xcode 9 Beta 5. Thanks, Lorenzo

---

<div class="post-metadata">

**Author:** ![rommex73](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rommex73/32/81494_2.png) [@rommex73](https://forums.kodeco.com/u/rommex73)\
**Post date:** [August 21, 2017, 12:34pm UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/15 "2017-08-21T12:34:04Z")

</div>

Hey @lorenzoboaro! Thanks for your reply. In fact, you boosted my interest and I installed Xcode Beta ))) So this is not a big deal. I am more concerned about Memento pattern, as I commented in the 2nd part. thanks for your work!!!

---

<div class="post-metadata">

**Author:** ![lorenzoboaro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lorenzoboaro/32/99103_2.png) [@lorenzoboaro](https://forums.kodeco.com/u/lorenzoboaro)\
**Post date:** [August 22, 2017, 3:15pm UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/16 "2017-08-22T15:15:56Z")

</div>

Hi @rommex73! Thanks again for your feedback! I’ve replied to your second question. Let me know if you need further explanations. Thanks, Lorenzo

---

<div class="post-metadata">

**Author:** ![gdubz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gdubz/32/88680_2.png) [@gdubz](https://forums.kodeco.com/u/gdubz)\
**Post date:** [August 24, 2017, 7:18pm UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/17 "2017-08-24T19:18:27Z")

</div>

In **Xcode 8.3.3** you can’t even open the **Main.storyboard** file to get to the point where you see the **Interface Builder Document** section of the **File Inspector** tab.

Is there no way to compile this project in Xcode 8.3.3? I already dropped the deployment target to iOS 10.3 and changed the language back to Swift 3.

---

<div class="post-metadata">

**Author:** ![lorenzoboaro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lorenzoboaro/32/99103_2.png) [@lorenzoboaro](https://forums.kodeco.com/u/lorenzoboaro)\
**Post date:** [August 25, 2017, 7:48am UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/18 "2017-08-25T07:48:37Z")

</div>

Hi @gdubz! Thanks for your feedback. The project has been created with Xcode 9 beta. You can download it from [https://developer.apple.com/download/](https://developer.apple.com/download/). Swift 4 has been also used. Let me know if you need further info. Thanks, Lorenzo

---

<div class="post-metadata">

**Author:** ![kyawhtetsoe](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@kyawhtetsoe](https://forums.kodeco.com/u/kyawhtetsoe)\
**Post date:** [October 7, 2017, 8:12am UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/19 "2017-10-07T08:12:45Z")

</div>

I copy pasted the question I asked in stackoverflow.  
I am following tutorial from raywanderlich site to implement singleton and facade design pattern from this link. [https://www.raywenderlich.com/160651/design-patterns-ios-using-swift-part-12](https://www.raywenderlich.com/160651/design-patterns-ios-using-swift-part-12) In the tutorial, they hardcoded album data inside init() of the PersistencyManager class. However, in my case I need to call http request to get the data. So the problem now is when the libraryAPI (singleton) create PersistencyManger instance, the init() gonna take few times to fetch the data. So, when my viewcontroller want to call libraryAPI.getAlbum(), it’s gonna be empty. Also I still need to reload data for my viewcontroller. So what’s the best way to implement facade design pattern with http request? I don’t want to do completion block in my viewcontroller, hence I believe the purpose of the facade design patter to hide any thing complex for the viewcontroller

---

<div class="post-metadata">

**Author:** ![diana\_mew](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@diana\_mew](https://forums.kodeco.com/u/diana_mew)\
**Post date:** [January 18, 2018, 4:24pm UTC](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015/20 "2018-01-18T16:24:44Z")

</div>

Great article! For those who want more found some repos with great implementations:  
in swift 4: [https://github.com/oleh-zayats/design-patterns-swift](https://github.com/oleh-zayats/design-patterns-swift),  
more detailed and practical: [kingreza (kingreza) / Repositories · GitHub](https://github.com/kingreza?tab=repositories)

[Next page](https://forums.kodeco.com/t/design-patterns-on-ios-using-swift-part-1-2/33015.md?page=2)
