Reputation: 3952
While following a tutorial on using the Keychain feature, I noticed a section of code where I needed to implement a structure as the following:
// Keychain Configuration
struct KeychainConfiguration {
static let serviceName = "TouchMeIn"
static let accessGroup: String? = nil
}
I know that a constant property on a value type can't be modified once instantiated so I was curious of the purpose of using static in this sense?
P.S.
This question is not similar to this question because the highest accepted answer (which I would think is the best answer) doesn't provide enough detail or any pros or cons.
Upvotes: 1
Views: 145
Reputation: 3082
It has multiple applications, including but not limited by the following:
1) To give a constant separate namespace, if constants have same names.
struct A {
static let width: Int = 100
}
struct B {
static let width: Int = 100
}
print(A.width)
print(B.width)
2) Static constants are 'lazy' by design, so if you are about to use lazy-behaved global constant, it might be handy to put it in a structure.
3) To show your coworkers that constant is applicable to specific domain where given structure is used.
4) Organize your configuration in sections:Theme.Layout.itemHeight
or Label.Font.avenirNext
Upvotes: 2