Swift: a global constant naming convention?

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

For example:

let maximumNumberOfLoginAttempts = 10 

Is it correct?

I use all caps, for example, MAXIMUM_NUMBER_OF_LOGIN_ATTEMPTS , from C, but I want to agree to Swift conventions.

+46
naming-conventions constants swift global
Jun 16
source share
5 answers

The Swift 3 API instructions state that "type and protocol names are UpperCamelCase. Everything else is lowerCamelCase."

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

Ideally, your global constants will be located inside some structure that will be UpperCamelCase, and all properties in this structure will be lowerCamelCase

 struct LoginConstants { static let maxAttempts = 10 } 

And so it happened

 if attempts > LoginConstants.maxAttempts { ...} 
+30
Jun 28 '16 at 14:24
source share

I discussed using camel housing with lead capital for class level constants. For example:

 static let MaximumNumberOfLoginAttempts = 10 

This is still a camel case (as Apple recommends), but the leading capital character makes it clear that the meaning is the same.

+19
Aug 08 '15 at 14:14
source share

Apple stands for CamelCase. However, many use _camelCase only to distinguish it, especially if you have the same name in a lower scope.

+5
Jan 20 '15 at 4:48
source share

I usually see constants declared with k , for example:

 static let kLoginAttemptsMax = value 

It also follows the camel shell to the "T".

+4
Jan 25 '16 at
source share

Apple shows us constants with camelCase.

I use the readable option. So for your example:

 let maximumNumberOfLoginAttempts = 10 let MAXIMUM_NUMBER_OF_LOGIN_ATTEMPTS = 10 

The 'MAXIMUM_NUMBER_OF_LOGIN_ATTEMPTS' ist is read to me and it immediately shows that it is a constant var.

+2
Jun 16 '14 at 14:13
source share



All Articles