-5

I'm following tutorial for Swift 4 and have found the usages of 'let' or 'var' in Swift quite inconsistent.

//in try catch : "let ... as" to match a error ?
catch let(or var) printerError as PrinterError
//in switch 1: "let .." to match a case pattern ?
case let(or var) .result(sunrise, sunset):
//in switch 2: "let ... where" pointless for me, why not just use someVar.hasSuffix ?
switch: someVar {
case let x(or var) where x.hasSuffix("pepper"):

Could anyone kindly give a summary of the usage in Swift?


It seems everyone is answering about the difference between 'let' and 'var' and mark the question as a duplication. But, I didn't even mention anything about 'var' in the original post at the first place!

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Di Huan
  • 26
  • 3
  • possible duplicate of https://stackoverflow.com/questions/24002092/what-is-the-difference-between-let-and-var-in-swift – user3581248 Sep 08 '17 at 07:29
  • 2
    It's not just `let`. It's `catch let` and `case let`. Please, please read the [Swift Language Guide](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309), the basics, error handling and control flow / pattern matching. Everything is described in detail. – vadian Sep 08 '17 at 07:33
  • `let` is for constants: variables that won't be altered. You could just as well use `var` but `let` saves memory allocation. – LinusGeffarth Sep 08 '17 at 07:33
  • Possible duplicate of [What is the difference between \`let\` and \`var\` in swift?](https://stackoverflow.com/questions/24002092/what-is-the-difference-between-let-and-var-in-swift) – MrX Sep 08 '17 at 08:51
  • @vadian thx, i will check that. But I am still find it hard to get the design logic for these usages. – Di Huan Sep 12 '17 at 01:13

3 Answers3

1

let is keyword that is used to declare a constant value of any data type, using let you can declare a value but you can't change its value again through out the project and if you try to change its value, it will gives you an error stating that this is a let constant. If you want to modify its value kindly change it to var, where var is a keyword used for variables.

let x: Int = 5
let string : String = "Hello! World"

The above values are constant and you can never change these values.

var x: Int = 5
var string: String = "Hello! World"

The above values are variables. You can change their value anywhere in the code.

Anshul Bhatheja
  • 673
  • 3
  • 21
1

let is used for constants while var is for variables

let is also used for optional binding like in your examples. You use optional binding to find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable.

dhin
  • 460
  • 5
  • 12
0

Let is a keyword to declare a constant. Think of constant as a box that stores information.

let name = "Bob"

"let" is the keyword to declare the constant.

"name" is the name you assign your constant. This is the box you store your information in. You can name it whatever you want doesn't have to be "name"

"=" assigns the value(your information) from the right side to the "name" constant.

"Bob" is the value aka the information you want to store. It can be anything you want and it's assigned to your constant.

Something you have to remember for constant is that constants are immutable. Meaning the values once declared cannot be changed. That's why it's called constant, because the values are always constant and does not change.

Kwanster
  • 1
  • 2