Completed 100 Days of Swift - Day #5
parent
3f490023fa
commit
0b1a5057dc
|
@ -0,0 +1,10 @@
|
|||
// Accepting parameters
|
||||
// https://www.hackingwithswift.com/sixty/5/2/accepting-parameters
|
||||
|
||||
print("Hello, world!")
|
||||
|
||||
func square(number: Int) {
|
||||
print(number * number)
|
||||
}
|
||||
|
||||
square(number: 8)
|
|
@ -0,0 +1,13 @@
|
|||
// Default parameters
|
||||
// https://www.hackingwithswift.com/sixty/5/6/default-parameters
|
||||
|
||||
func greet(_ person: String, nicely: Bool = true) {
|
||||
if nicely == true {
|
||||
print("Hello, \(person)!")
|
||||
} else {
|
||||
print("Oh no, it's \(person) again...")
|
||||
}
|
||||
}
|
||||
|
||||
greet("Taylor")
|
||||
greet("Taylor", nicely: false)
|
|
@ -0,0 +1,8 @@
|
|||
// Omitting parameter labels
|
||||
// https://www.hackingwithswift.com/sixty/5/5/omitting-parameter-labels
|
||||
|
||||
func greet(_ person: String) {
|
||||
print("Hello, \(person)!")
|
||||
}
|
||||
|
||||
greet("Taylor")
|
|
@ -0,0 +1,14 @@
|
|||
// Parameter labels
|
||||
// https://www.hackingwithswift.com/sixty/5/4/parameter-labels
|
||||
|
||||
func square(number: Int) -> Int {
|
||||
return number * number
|
||||
}
|
||||
let result = square(number: 8)
|
||||
|
||||
// -------------------------------------------------------------
|
||||
|
||||
func sayHello(to name: String) {
|
||||
print("Hello, \(name)!")
|
||||
}
|
||||
sayHello(to: "Taylor")
|
|
@ -0,0 +1,16 @@
|
|||
# Day #5
|
||||
|
||||
---
|
||||
|
||||
**What I learned about**
|
||||
|
||||
- **[Writing functions](/100DaysOfSwift/Day5/Writing%20functions.swift)**
|
||||
- **[Accepting parameters](/100DaysOfSwift/Day5/Accepting%20parameters.swift)**
|
||||
- **[Returning values](/100DaysOfSwift/Day5/Returning%20values.swift)**
|
||||
- **[Parameter labels](/100DaysOfSwift/Day5/Parameter%20labelss.swift)**
|
||||
- **[Omitting parameter labels](/100DaysOfSwift/Day5/Omitting%20parameter%20labels.swift)**
|
||||
- **[Default parameters](/100DaysOfSwift/Day5/Default%20parameters.swift)**
|
||||
- **[Variadic functions](/100DaysOfSwift/Day5/Variadic%20functions.swift)**
|
||||
- **[Writing throwing functions](/100DaysOfSwift/Day5/Writing%20throwing%20functions.swift)**
|
||||
- **[Running throwing functions](/100DaysOfSwift/Day5/Running%20throwing%20functions.swift)**
|
||||
- **[inout parameters](/100DaysOfSwift/Day5/inout%20parameters.swift)**
|
|
@ -0,0 +1,9 @@
|
|||
// Returning values
|
||||
// https://www.hackingwithswift.com/sixty/5/3/returning-values
|
||||
|
||||
func square(number: Int) -> Int {
|
||||
return number * number
|
||||
}
|
||||
|
||||
let result = square(number: 8)
|
||||
print(result)
|
|
@ -0,0 +1,11 @@
|
|||
// Running throwing functions
|
||||
// https://www.hackingwithswift.com/sixty/5/9/running-throwing-functions
|
||||
|
||||
do {
|
||||
try checkPassword("password")
|
||||
print("That password is good!")
|
||||
} catch {
|
||||
print("You can't use that password.")
|
||||
}
|
||||
|
||||
// do starts a section of code that might cause problems, try is used before every function that might throw an error, and catch lets you handle errors gracefully.
|
|
@ -0,0 +1,11 @@
|
|||
// Variadic functions
|
||||
// https://www.hackingwithswift.com/sixty/5/7/variadic-functions
|
||||
|
||||
print("Haters", "gonna", "hate")
|
||||
|
||||
func square(numbers: Int...) {
|
||||
for number in numbers {
|
||||
print("\(number) squared is \(number * number)")
|
||||
}
|
||||
}
|
||||
square(numbers: 1, 2, 3, 4, 5)
|
|
@ -0,0 +1,15 @@
|
|||
// Writing function
|
||||
// https://www.hackingwithswift.com/sixty/5/1/writing-functions
|
||||
|
||||
func printHelp() {
|
||||
let message = """
|
||||
Welcome to MyApp!
|
||||
|
||||
Run this app inside a directory of images and
|
||||
MyApp will resize them all into thumbnails
|
||||
"""
|
||||
|
||||
print(message)
|
||||
}
|
||||
|
||||
printHelp()
|
|
@ -0,0 +1,14 @@
|
|||
// Writing throwing functions
|
||||
// https://www.hackingwithswift.com/sixty/5/8/writing-throwing-functions
|
||||
|
||||
enum PasswordError: Error {
|
||||
case obvious
|
||||
}
|
||||
|
||||
func checkPassword(_ password: String) throws -> Bool {
|
||||
if password == "password" {
|
||||
throw PasswordError.obvious
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
// inout parameters
|
||||
// https://www.hackingwithswift.com/sixty/5/10/inout-parameters
|
|
@ -13,4 +13,5 @@
|
|||
- **[Day #1](/100DaysOfSwift/Day1/)** - variables, simple data types, and string interpolation
|
||||
- **[Day #2](/100DaysOfSwift/Day2/)** - arrays, dictionaries, sets, and enums
|
||||
- **[Day #3](/100DaysOfSwift/Day3/)** - operators and conditions
|
||||
- **[Day #4](/100DaysOfSwift/Day4/)** - loops, loops, and more loops
|
||||
- **[Day #4](/100DaysOfSwift/Day4/)** - loops, loops, and more loops
|
||||
- **[Day #5](/100DaysOfSwift/Day5/)** - functions, parameters, and errors
|
|
@ -1,7 +1,17 @@
|
|||
# [100 Days of SwiftUI](https://www.hackingwithswift.com/100/swiftui)
|
||||
# [100 Days of Swift](https://www.hackingwithswift.com/100/swiftui)
|
||||
|
||||
---
|
||||
|
||||
**This folder is for the [100 Days of SwiftUI](https://www.hackingwithswift.com/100/swiftui) challenge.**
|
||||
|
||||
**NOTE:** This is **[100 Days of SwiftUI](https://www.hackingwithswift.com/100/swiftui)**, not **[100 Days of Swift](https://www.hackingwithswift.com/100)** which is located [here](/100DaysOfSwift/)
|
||||
|
||||
---
|
||||
|
||||
**Days 1-12: Introduction to Swift**
|
||||
|
||||
- **[Day #1](/100DaysOfSwift/Day1/)** - variables, simple data types, and string interpolation
|
||||
- **[Day #2](/100DaysOfSwift/Day2/)** - arrays, dictionaries, sets, and enums
|
||||
- **[Day #3](/100DaysOfSwift/Day3/)** - operators and conditions
|
||||
- **[Day #4](/100DaysOfSwift/Day4/)** - loops, loops, and more loops
|
||||
- **[Day #5](/100DaysOfSwift/Day5/)** - functions, parameters, and errors
|
Loading…
Reference in New Issue