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
// This is an optional value | |
var box : String? | |
// This is an optional value set to an actual value | |
var box : String? = "Something" |
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
var box: String? = "Something" | |
>>> | |
print(box!) | |
--- | |
"Something" | |
var box: String? | |
>>> | |
print(box!) | |
--- |
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
var box: String? | |
print(box!) |
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
// Within a function | |
var box: String? = "Something" | |
guard let boxContents = box else { return } | |
print(boxContents) | |
--- | |
"Something" |
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
// Ran in the same codeblock | |
var box1: String? | |
if let boxContents = box1 { | |
print(boxContents) | |
} | |
--- | |
Nothing Prints | |
var box2: String? = "Something" | |
if let boxContents = box2 { |
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
protocol MomDelegate { | |
func washDishes() | |
func cleanRoom() | |
func walkNala() | |
} |
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 Son: MomDelegate { | |
func washDishes() { | |
print("Washing Dishes") | |
} | |
func cleanRoom() { | |
print("Cleaning Room") | |
} | |
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 Mom { | |
var _son: Son | |
init(son: Son) { | |
self._son = son | |
} | |
func tellJoshWhatToDo() { | |
self._son.washDishes() | |
self._son.cleanRoom() |
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 josh = Son() | |
let joshsMom = Mom(son: josh) | |
joshsMom.tellJoshWhatToDo() | |
--- | |
"Washing Dishes" | |
"Cleaning Room" | |
"Walking Nala" |
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
// Most verbose: No compiler assumptions are made, the closure to map | |
// receives a transforming function explicitly casted as (val: Int) -> Int | |
// Transforms an array of Int and multiplies each value by 10 | |
arrValues.map({ (val: Int) -> Int in | |
return val * 10 | |
}) | |
// Compiler assumes return type for closure, no need to explicitly indicate | |
arrValues.map({ (val: Int) in | |
return val * 10 |
OlderNewer