Welcome back!
I know it’s obvious we all want to make awesome things. And with amazing new frameworks like ARKit, Vision or Core ML there’s a lot of stuff to discover. But today I want to talk about another topic. I’m coding for almost 2,5 years now, and one of things I found (and find) most complicated is getting the right architecture for my projects. As a beginning programmer I didn’t start off with creating the perfect architectures. If the app was doing what it supposed to do, I was the luckiest man on the face of the earth. But since I became a bit addicted to coding, I also wanted to do it the right way, with design patterns that make sense. What made it more difficult for me was the fact that I had a hard time finding the right articles and books about this stuff.
Anyway, after a while I got a bit of hang on what’s good and bad practice. And with a little help form my coding friends I also find some good blogs about the topic. To name one, I like this one about SOLID programming principles by Marco Santarossa.
To be totally honest, I still have got a lot to learn. Still I wanted to share some things I have found out, starting today. Let’s begin with a simple one;
What’s a proper architecture for putting some static data in your project?
Say, I have an app which contains some ’Tips & tricks’ brought to you by some Zen master. Our aim is to display this tips in a random way to the user in a viewController. The way to achieve this is actually (which may not be a surprise) quite simple.
- We create a struct named Tip which contains the various properties of a tip we want to describe
- We create a class TipsHelper to create some actual tips and give them back in a random fashion
- We inject an instance of Tip into the VC, where it is used to give content to the views.
Creating the struct
This one is simple. The advantage using a struct and not a class is that a struct does not need initializers. The only question is; what aspects of a tip do we need (which properties should our struct contain)? In this case I figured out a Tip always have a title, some text (the actual tip itself) and optionally an image.
1 2 3 4 5 6 7 8 9 10 11 |
import Foundation import UIKit struct Tip { var tipTitle: String var tipText: String var tipImage: UIImage? } |
Creating the class
So we have our Tip-struct, but we still we don’t have any actual tips yet. So, let’s get this over with right away. We are going to create a separate class that we name TipsHelper. We create some tips with a tipTitle and some tipText. We don’t use the image-property by filling in nil (remember, the tipImage property is optional, which means we don’t have to provide an image!).
Okay, we have our tips now, but we still don’t have a way in which they can be presented randomly. We need to create a function that gives back a random Tip.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import Foundation import UIKit class TipsHelper { static var tips: [Tip] = [ Tip(tipTitle: "Wees specifiek!", tipText: "Omschrijf je probleem altijd zo specifiek mogelijk aan de hand van een concrete gebeurtenis. Doe dit in jouw eigen woorden, zodat het bij jou past. Beschrijf vervolgens de intensiteit van jouw probleem aan de hand van de reactie in jou lichaam. Een voorbeeld: [arrogante baas die mij uitschold dinsdag middag, dit voel ik in mijn buik] of [mijn partner mij negeerde bij het avondeten, dit voel ik alsof mijn keel dicht geknepen wordt", tipImage: nil), Tip(tipTitle: "Probeer het op alles uit!",tipText: "Denk hierbij aan emoties als stress, angst en somberheid. Je kan Smarttapping ook toepassen op beelden en geluid. Eigenlijk maakt het niet uit waar je last van hebt. Onderzoek hoe het ook bij jou in beweging kan komen door Smarttapping.", tipImage: nil), Tip(tipTitle: "Neem je tijd",tipText: "Sluit je ogen even voordat je gaat tappen en probeer je probleem zo levendig mogelijk te zien / voelen / horen. Speel het moment nog even af en ga pas met de opstartzin starten als je goed voelt.", tipImage: nil), Tip(tipTitle: "Registreer",tipText: "Registreer altijd je issues bijvoorbeeld in je SHITLIST. Zo monitor je jouw vooruitgang. Elke stap is er weer één! Het opschrijven van jouw lijst met problemen geeft helderheid en motiveert je jouw problemen op te ruimen.", tipImage: nil), Tip(tipTitle: "Blijf doorgaan!",tipText: "Smarttapping werkt snel, soms zelfs al na 1 ronde! Laat je echter niet verleiden door de 1-minuut trick, want vaak hebben problemen gewoon wat meer rondes nodig. Dus gewoon nog een ronde, net zolang het rustig is, je score naar 0 is of jij voelt dat het goed is.", tipImage: nil), ] static func getRandomTip() -> Tip { let randomIndex = Int(arc4random_uniform(UInt32(tips.count))) return tips[randomIndex] } } |
Implementing it in the VC
The only thing left for now is to inject the tips in the viewController in which we need them. In this case we use them to give in the texts of 2 labels.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
override func viewDidLoad() { super.viewDidLoad() cancelButton.setTitle("Ga verder", for: .normal) tiptTitleLabel.text = "tipsvc.titlelabel.to_van_merel".localized let tip = TipsHelper.getRandomTip() tipContentLabel.text = tip.tipText titleLabel.text = tip.tipTitle } |
And that’s all for today. This example is very simple, but I think it gives a very good idea how to separate your code in a proper way. If you have any suggestions for better or alternative ways, please let me know. I’m still learning!
Happy coding!