From 1d5fed62f1eb13d2e3735c7daed5597ac5b679ea Mon Sep 17 00:00:00 2001 From: Marwan Alani Date: Sun, 14 Oct 2018 12:26:14 -0400 Subject: [PATCH] feat(guide): Explained the difference between declaring & assigning a constant in Swift (#18515) Added a code snippet to explain the difference between _declaring_ a constant and _assigning_ a value to it. --- .../src/pages/guide/english/swift/constants/index.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/client/src/pages/guide/english/swift/constants/index.md b/client/src/pages/guide/english/swift/constants/index.md index 61b24df49d..96c11f99fe 100644 --- a/client/src/pages/guide/english/swift/constants/index.md +++ b/client/src/pages/guide/english/swift/constants/index.md @@ -14,7 +14,16 @@ You declare constants with the `let` keyword then give it a name `name` then you Once you have declared a constant you don't need to use the `let` keyword anymore you just call it by its name. -The value of a constant can’t be changed once it’s set. +The value of a constant can’t be changed once it’s _set_. With that being said, it is important to note that the Swift compiler is smart enough to understand the difference between _declaring_ a constant, and _assigning_ a value to it. Consider the following code snippet: +```swift +let shouldWaterFreeze: Bool // (1) +if temperature < 0 { + shouldWaterFreeze = true // (2) +else { + shouldWaterFreeze = false // (3) +} +``` +The snippet above is valid and compiles with no problems (given that we have declared and assigned an `Int` value to `temparature` somewhere earlier). Notice that we _declare_ the constant in (1), and then _assign_ a value to it (2) or (3). #### More Information: - The Swift Programming Language