In Swift, it seems that global constants should be camelCase.
For example:
let maximumNumberOfLoginAttempts = 10
Is that correct?
I'm used to all caps, eg, MAXIMUM_NUMBER_OF_LOGIN_ATTEMPTS
, from C, but I want to acquiesce to Swift conventions.
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 { ...}
I've been debating using camel case with a leading capital for class-level constants. For example:
static let MaximumNumberOfLoginAttempts = 10
It's still camel-case (as Apple appears to recommend), but the capitalized leading character makes it clear that the value is immutable.
Apple advocates the camelCase. That said, many use _camelCase just to differentiate it especially if you are likely to have the same name at a lower scope.
I commonly see constants declared with a k
, like so:
static let kLoginAttemptsMax = value
This also follows camel casing to a "T".
To improve on @bmjohns answer, it's better to use an enum
instead of a struct
to act as a namespace for your constants. An enum
with no cases can't be instantiated, whereas a struct
can. If it is a struct
, then instantiating it (via LoginConstants()
) is allowed, but that has no meaning and doesn't make sense to do.
The convention is to use enumerations for namespacing, as such:
enum LoginConstants {
static let maxAttempts = 10
}
This ensures that the only valid usage of LoginConstants
is for accessing its static members.
There are a couple of options.
thisIsMyConstant
.
Option+Click
.UIColor.myColor
instead of UIColor.MyColor()
way before Apple made the change, including me.Objective-C
style kThisIsMyConstant
. As a lot of Swift
developers are objc
experts, it should be pretty obvious for most Mac/iOS developers.C
style THIS_IS_MY_CONSTANT
. It's also used by a lot of other languages like Java
.ThisIsMyConstant
. I can't think of any languages using this style for now (there should be some but I just can't recall), but it's kind of close to Apple's suggestion.Edit: it also depends on your linting/autoformatting tool(s). More thoughts were added as comments.
Apple shows us constants with camelCase.
I use the bether readable variant. So for your example:
let maximumNumberOfLoginAttempts = 10
let MAXIMUM_NUMBER_OF_LOGIN_ATTEMPTS = 10
'MAXIMUM_NUMBER_OF_LOGIN_ATTEMPTS' ist bether readable for me and it shows instantly, that it's a constant var.
You can see not just what apple says, but what they do.
You can have a look at Foundation class, that is a class created by apple and it is imported by default in the ViewController file when you create a new project.
Press command+optionn+clic over the class to see its declaration.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.