@@ -82,6 +82,60 @@ because the maximum value never changes.
8282The current login attempt counter is declared as a variable,
8383because this value must be incremented after each failed login attempt.
8484
85+ If a stored value in your code won't change,
86+ always declare it as a constant with the ` let ` keyword.
87+ Use variables only for storing values that change.
88+
89+ When you declare a constant or a variable,
90+ you can give it a value as part of that declaration,
91+ like the examples above.
92+ Alternatively,
93+ you can assign its initial value later in the program,
94+ as long as it's guaranteed to have a value
95+ before the first time you read from it.
96+
97+ ``` swift
98+ var environment = " development"
99+ let maximumNumberOfLoginAttempts: Int
100+ // maximumNumberOfLoginAttempts has no value yet.
101+
102+ if environment == " development" {
103+ maximumNumberOfLoginAttempts = 100
104+ } else {
105+ maximumNumberOfLoginAttempts = 10
106+ }
107+ // Now maximumNumberOfLoginAttempts has a value, and can be read.
108+ ```
109+
110+ <!--
111+ - test: `constantsWithDeferredInitialization`
112+
113+ ```swifttest
114+ -> var environment = "development"
115+ -> let maximumNumberOfLoginAttempts: Int
116+ -> if environment == "development" {
117+ maximumNumberOfLoginAttempts = 100
118+ } else {
119+ maximumNumberOfLoginAttempts = 10
120+ }
121+ >> print(maxNumberOfLoginAttempts)
122+ << 100
123+ ```
124+ -->
125+
126+ In this example,
127+ the maximum number of login attempts is constant,
128+ and its value depends on the environment.
129+ In the development environment,
130+ it has a value of 100;
131+ in any other environment, its value is 10.
132+ Both branches of the ` if ` statement
133+ initialize ` maximumNumberOfLoginAttempts ` with some value,
134+ guaranteeing that the constant always gets a value.
135+ For information about how Swift checks your code
136+ when you set an initial value this way,
137+ see < doc:Declarations#Constant-Declaration > .
138+
85139You can declare multiple constants or multiple variables on a single line,
86140separated by commas:
87141
@@ -99,10 +153,6 @@ var x = 0.0, y = 0.0, z = 0.0
99153 ```
100154-->
101155
102- > Note: If a stored value in your code won't change,
103- > always declare it as a constant with the ` let ` keyword.
104- > Use variables only for storing values that need to be able to change.
105-
106156### Type Annotations
107157
108158You can provide a * type annotation* when you declare a constant or variable,
0 commit comments