Syntax

TOPICS


March 2019

  Swift basic concepts - Functions and Classes
March 7   |   Swift

 Functions and Classes

            


// Functions
func grettings_void() {
print("Hello World!")
}

func grettings() -> String {
return "Hello World!"
}

func grettings(yourname:String) -> String {
return "Hello \(yourname)"
}

func sum(a:Int,b:Int) -> Int {
return a + b
}

print(grettings_void())
print(grettings())
print(grettings(nombre: "Jorge"))
print(sum(a: 2, b: 3))

// Classes
class Person {
var name : String
var age : Int
  
 init(\_name:String, \_age:Int) {
self.name = \_name
self.age = \_age
}
  
 func grettings() -> String {
return self.name + " says Hi!"
}
}

let person = Person(\_name: "Jorge", \_age: 21)
print("This person is named \(persona.nombre) and is \(persona.edad) years old")
print(person.grettings())

            
            
        
  Swift basic concepts - Control and data
March 6   |   Swift

 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

            
            
        
  Swift basic concepts - Variables
March 5   |   Swift

 Variables

            

// Variable declaration
var expressiveAssigment : String = "Value"
print(expressiveAssigment)

var expressive : String = ""
expressive = "Value"
print(expressive)

var intuitive = "Guest What?"
print(intuitive)

// Contants
let constantAssigment : String = "Constant Value"
print(constantAssigment)

let pi = 3.1416
print(pi)

// Optionals
var optionalString : String? = nil
var optional : Int? = nil

// How to work with optionals
print(optional) // nil
print(optional ?? 0) // Provide a default value
print(optional as Any) // Explicit Cast to Any to avoid errors
print(optional!) // Force-unwrap. CAUTION: Throw error if value is nil

// Optional Binding
if let opcbin = opcional {
print(opcbin)
} else {
print("No value")
}

// Strings
var phrase = "World!"
print("Hello " + phrase)
print("Hello ",phrase)
print("Hello \(phrase)")

let stringMultiline = """
To create a multine text
we must use three quotes
the end quotes must be on a new line
"""

let simpleCharacter : Character = "A"

// Cmd + Ctrl + Space
var emojis = "😇"

// Concatenation
emojis.append(simpleCharacter)

// String Functions
emojis.count
emojis.isEmpty
stringMultilinea.prefix(10)

// Swift 5 - Raw Strings
var rawString = #"Use "quotes" without escape sequences"#
var rawString2 = #"Use "quotes" along with a variable \#(phrase)"#