Use Enum For Your Swift Constants
I’ve started doing this!
Yes, we have all read articles about the use of Swift’s language features. I would claim that things like Swift’s constants are actually reasonably easy to implement. But how would I actually use them in a project?
Remembering the format
I think Constants should always be nouns in the lowerCamelCase
naming convention,
A variable or a constant?
A variable can be changed in your class or struct. It is declared as something like the following:
var padding: CGFloat = 10.0
A constant is fixed, and can be declared as something like the following:
let padding: CGFloat = 10.0
Which is a closed case. Right? RIGHT?
It’s not enough for me
So when I’m using something like padding, I’d put that padding in the ViewController of my MVVM architecture, and have it as a Constant which cannot be changed.
This would make my ViewController something like:
final class PhotoListViewController: UIViewController {
private enum Constants {
static let padding: CGFloat = 10.0
}
...