We can convert a 8 bit unsigned integer to an 16 bit one much like you do in Python
let twoThousand: UInt16 = 2_000
let one: UInt8 = 1
let twoThousandAndOne = twoThousand + UInt16(one)
Unpacking in Python is called Decomposing here
let http200 = (200, "OK")
let (status_code, message) = http200
Similary you can also ignore values using _
, this is called a Wildcard Pattern
Somewhat like the whalrus operator once can have an assigment operation inside a conditional
let numStr = "123"
if let numInt = Int(numStr) {
print("The string \"\(numStr)\" has an integer value of \(numInt)")
} else {
print("The string \"\(numStr)\" couldn't be converted to an integer")
}
Passing named parameters to a Swift function follows the type annotatin syntax
func greet(person: String) -> String {
let greeting = "Hello, " + person
return greeting
}
Is called like
greet(person: "As3tic")
A Wildcard pattern can match any value and then ignore it. Like in Python you use one when the value in question doesn’t matter.
for _ in 1...3 {
// Do something three times.
}