Swift: Global constant naming convention?

ma11hew28 picture ma11hew28 · Jun 16, 2014 · Viewed 24.9k times · Source

In Swift, it seems that global constants should be camelCase.

For example:

let maximumNumberOfLoginAttempts = 10

Is that correct?

I'm used to all caps, e.g., MAXIMUM_NUMBER_OF_LOGIN_ATTEMPTS, from C, but I want to acquiesce to Swift conventions.

Answer

bmjohns picture bmjohns · Jun 28, 2016

Swift 3 API guidelines state that "Names of types and protocols are UpperCamelCase. Everything else is lowerCamelCase."

https://swift.org/documentation/api-design-guidelines/

Ideally your global constants will be located within a struct of some sort, which would be UpperCamelCase, and all properties in that struct would be lowerCamelCase

struct LoginConstants {
    static let maxAttempts = 10
}

And accessed like so,

if attempts > LoginConstants.maxAttempts { ...}