Swift: Private Constants vs Constant Type Properties

1.8k Views Asked by At

Should I use a constant type property instead of a private constant?

At the top of my ChatViewController.swift file, outside of the class definition, I have a private constant:

private let messageFontSize: CGFloat = 17

But since this constant is only used by instances of ChatViewController, should I move it inside of the class definition and make it a constant type property?

class ChatViewController: UIViewController {
    static let messageFontSize: CGFloat = 17
    // . . .
}

But then, I'd have to access it rather verbosely:

ChatViewController.messageFontSize
1

There are 1 best solutions below

0
On

I would move it into the class regardless whether it is used somewhere else or not. There is always one class that sort of "owns" a constant. If the class makes it available to other classes then make it public (Well, in swift default would do in most cases). And yes, consequentally you will have to prefix it with the class' name.