Swift: Working with Constants
So you have stumble across the first in the many articles that I will be bringing to you to help you make your developments much faster and more efficient. This one in particular, is going to save you many hours of pain later on in the development lifecycle in any project, new or old; bearing in mind if it is old then the pain is probably already there.
Constants
A Constant, is a value that never changes, don’t get it mixed up with a variable as these can be manipulated, where-as constants are not.
Constants are parameters that we use all the time to build applications to meet the requirements given. The main ones that I always set up during the beginning of a GreenField project are the following:
- Colors
- Paddings and sizes
- Strings
- Images
- Fonts
Most applications require all the above, so why not set them up in a constant/theming folder at the start of the project, to match the requirements of design. It takes a short amount of time, but will save you hours later.
Using Swift Extensions, which allow you to add functionality to already existing classes that you will require, you can create whole sets of constants to suit your needs. So lets go ahead and start with sizes:
Sizes
These can be anything for button sizes, component edges or even animation settings:
struct Size { // MARK: Corners
static let smallCorner: CGFloat = 5
static let largeCorner: CGFloat = 12 // MARK: Paddings
static let smallPadding: CGFloat = 8
static let standardPadding: CGFloat = 16
static let largePadding: CGFloat = 24}
If you are working with designers you will more than likely find that they will be working with a particular set of rules that help them put together UI fast and efficient, also providing a level of consistency. So if you ask them they can help with setting these up.
Taking it a step further you can add more Size constants to help with animations:
// MARK: Animation
static let quickAnimation: TimeInterval = 0.2
static let standardAnimation: TimeInterval = 0.3
Specific component sizes:
// MARK: Component Sizes
static let largeButtonHeight: CGFloat = 60
static let appleButtonHeight: CGFloat = 44
Ok lets have an example of these in use:
extension UIView { func fadeIn(time: TimeInterval = Size.standardAnimation, alpha: CGFloat = 1, completion: (() -> Void)? = nil) {
UIView.animate(withDuration: time, animations: {
self.alpha = alpha
}, completion: { _ in
completion?()
})
}}
In this example you will see that I have extended UIView
with a helper method to animate the fade in of a view. You can see that the default of this animation time is Size.standardAnimation
, give it a go yourself.
Strings
Now, I have been through many phases of development with a variety of applications and have been required to localise them when they have hardcoded strings, is the most frustrating task on this earth. So lets look at an example of moving this out of the code:
extension String { // MARK: HomeScreen
static let hsHelloWorld: String = "Hello world"}
Example:
lblTitle.text = .hsHelloWorld
Easy right?!
What is great about removing strings away from the code, is you can have a library of strings that you can manage and make adjusts too, as and when required without hunting for them. You will also find that you use the same strings in multiple places so you can set up some of the more common:
extension String { // MARK: Common
static let ok: String = "Ok"
static let done: String = "Done"}
Now, it doesn’t have to stop there with strings, you can create a LocalisationService that can use content keys that will grab the string you want from where you are storing them, you can check this out later:
Think you are probably getting to grips with this by now, so I will only do one more example:
Colors
extension UIColor { static let primary: UIColor? = UIColor(named: "primary")
static let secondary: UIColor? = UIColor(named: "secondary")}
Example:
backgroundColor = .primary
As I said at the beginning it takes a moment to set everything up, but once you have it is a life saver and you will notice a huge increase in development speed; especially when design ask you to change the largeCorner
from 12 to 15.
Obviously there can be some issues, if you are using largeCorner for all components and you only need to change one, then it might be an idea to take a look and create a new constant. But do be careful about ending up down the route of standard, standardish, standardishish paddings!
Thanks for reading and let me know your thoughts, share your thoughts and send to friends/collegues. Also check out my other tutorials that will make your life much simpler 😁.
Happy dev’ing 😀