Swift basic concepts - Control and data
March 6,
2019
Control and Data
// >> Switch
let numericValue = 3
switch numericValue {
case 1:
print("The value is 1")
case 2:
print("The value is 2")
case 3...10: // Using ranges
print("The value is greater or equal to 3 but less than 10")
default:
print("The value is greater than 10")
}
let weekDays = "Martes"
switch weekDays {
case "monday","wednesday","friday":
print("Promo A")
case "tuesday","thursday":
print("Promo B")
case "saturday":
print("Promo C")
default:
print("No promo today")
}
// FOR
for i in 1...10 {
print("Counter \(i)")
}
let alphabet = "ABCDEFGHIJKLMN"
for letter in alphabet {
print(letter)
}
let myArray = ["A","B","C","D","E","F"]
for item in myArray {
print("Item:\(item)")
}
// ARRAYS
// Array can contain only values with the same type
var varArray = ["A","B","C","D","E","F"]
varArray.count
varArray[0]
varArray.append("G")
varArray.remove(at: 0)
varArray.removeLast()
varArray.insert("A", at: 0)
// TUPLES
// Tuples can containt values with different types
var myTuple = ("iPhone XS", 10, 9999.99, false)
let varTupleSingleData = miTupla.0 // iPhone XS
let (product, quantity, price, isPromo) = miTupla // Variable destructuring