kodeco.com Forums

Design Patterns on iOS using Swift – Part 1/2

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
2 Likes

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 :slight_smile: Nice work!

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

I’ve found more comprehensive explanation for Decorator.
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.

Nice post about design patterns on Swift :slight_smile:

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

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).

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

@ka_dze Thanks for sharing the decorator pattern by GoF!

@micazeve Thanks for sharing this link! Very useful :wink:

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

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! :]

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

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

Hema

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

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!

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

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!!!

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

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.

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/. Swift 4 has been also used. Let me know if you need further info. Thanks, Lorenzo

1 Like

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 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

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