[Swift] Var, Let and Tuple
var
- Values assigned to variables can be changed
var myVar = 10 // :Int
var x = 0.0, y = 0.0, z = 0.0
let
- Once assigned, it cannot be changed later
tyoe annotation and tyoe inference
var userId : Int = 10 // Int is type annotation
var pi = 3.14 // Double type inference
Tuple
- To temporarily bind multiple values to a single object
let myTuple = (10, 12.1, "Hi")
var getTuple = myTuple.2
print(getTuple)
// Hi
- Extract all values in the Tuple and assign them to variables or constants
let myTuple = (10, 12.1, "Hi")
let (myInt, myFloat, myString) = myTuple
print(myFloat)
// 12.1
- _ ignore the value
let myTuple = (10, 12.1, "Hi")
let (myInt, _, myString) = myTuple
- When creating a Tuple, you can assign a name to each value.
let myTuple = (id: 10, length: 12.1, message: "Hi")
print(myTuple.message);
// Hi