What is the difference between let and var in Swift?
Hi! I just started learning Swift and can't understand the difference between `let` and `var`. They both seem to be used for declaring variables, but I noticed that Xcode sometimes complains if I use the wrong one.
For example, I wrote this code:
```swift
var name = "Ivan"
name = "Petr" // works
let age = 25
age = 30 // error! Cannot assign to value: 'age' is a 'let' constant
```
Why does the first case work fine, but the second gives an error? I thought `let` and `var` were the same thing. Maybe `let` can't be changed at all? But then why is it needed?
I also noticed that in examples from the internet, they often use `let` for arrays and dictionaries, but then add elements to them. How does that work? Doesn't `let` forbid changes?
Please explain in simple terms, wha
No answers yet.
Log in to leave an answer