Day 8: Default Parameters and Error Handling

Day 8 Tutorial

Default parameters

funcgreet(name:String="Guest") {print("Hello, \(name)")
}greet()greet(name:"Emily")

If no name is provided, Swift uses "Guest".

Creating an error

enumPasswordError:Error {casetooShort
}

Throwing an error

funccheckPassword(_password:String)throws->String {ifpassword.count<6 {throwPasswordError.tooShort
    }return"Password is valid"
}

Handling an error

do {letresult=trycheckPassword("123")print(result)
}catch {print("The password is too short.")
}

Remember:


Day 9: Closures

Day 9 Tutorial

A closure is similar to a function without a name. It can be stored in a variable or passed into another function.