This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Double { //swift class | |
func half() -> Double { | |
return self / 2 | |
} | |
} | |
1.3.half() //0.6500000000000000 | |
1000.0.half() // 500 | |
Double(999).half() // 499.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if let laptop = programmer.laptop { // set the value of laptop if `programmer.laptop` is not nil and | |
// enter into if-case or enter into `else` case. | |
if let laptopFan = laptop.fan { // set the value of laptopFanName if `laptop.fan` is not nil and | |
// enter into if-case or enter into `else` case. | |
print("\(laptop.name) contains \(laptopFan.name) fan") | |
} else { | |
print("\(laptop.name) contains no fan") // Note: String interpolation "\(value)" | |
} | |
} else { | |
print("\(programmer.name) have no laptop") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func printSummary(_ programmer: Programmer) { | |
guard let laptop = programmer.laptop else { // set the value of laptop if `programmer.laptop` is not nil | |
print("\(programmer.name) have no laptop") // and will go line 6. If `programmer.laptop` is nil, it will | |
return // enter into else case and return to caller of this function. | |
} | |
guard let laptopFan = laptop.fan else { // Syntax | |
print("\(laptop.name) contains no fan") // guard let constant = expression else { | |
return // return returnValue | |
} // } | |
print("\(laptop.name) contains \(laptopFan.name) fan") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fanNames = computerLab.laptops | |
.filter({laptop in !laptop.name.contains("Mac") }) | |
.filter({laptop in laptop.fan != nil}) | |
.map({laptop in laptop.fan!.name}) | |
// Simplified | |
let fanNames = computerLab.laptops | |
.filter({laptop in !laptop.name.contains("Mac") }) | |
.compactMap({laptop in laptop.fan?.name}) //compactMap will filter out nil values by default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// mandatory argument names for caller | |
func add(augend: Int, addend: Int) -> Int { | |
return augend + addend | |
} | |
let sum = add(augend: 5, addend: 2) | |
// no argument names for caller (underscore for no name) | |
func add(_ augend: Int, _ addend: Int) -> Int { | |
return augend + addend | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if case .sugar = sugarcane.taste { // if sugarcane.taste is `.sugar` then | |
print("yummmmy sugarcane") // enters into if | |
} | |
if case .salty(let value) = frenchFries.taste { // frenchFries.taste is .salty(10), so it matches | |
if value < 10 { // the case and assign the value 10 to `value` | |
print("manageable") // we can use the `value` inside the `if case` | |
} else { | |
print("impossible") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Dog { | |
var name: String | |
var weight: Int | |
} | |
var myDog = Dog(name: "Ranger", weight: 10) | |
var yourDog = myDog //Deep copy | |
yourDog.name = "Puncher" | |
print(myDog) // Dog(name: "Ranger", weight: 10) | |
print(yourDog) // Dog(name: "Puncher", weight: 10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dog { | |
var name: String | |
var weight: Int | |
// constructor or initializer | |
init(name: String, weight: Int) { | |
self.name = name | |
self.weight = weight | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Taste { | |
case sweet | |
case bitter | |
case sour | |
case spicy | |
case salty(Int) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Cherry: Eatable { | |
var taste: Taste = Taste.sweet | |
func isBitter() -> Bool { | |
return self.taste == .bitter | |
} | |
} | |
let 🍒 = Cherry() // literally we can have variable name like this🤓 | |
struct Chilly: Eatable { | |
var taste: Taste = Taste.spicy |