Skip to content

Instantly share code, notes, and snippets.

View josh-marasigan's full-sized avatar

Josh Marasigan josh-marasigan

View GitHub Profile
// This is an optional value
var box : String?
// This is an optional value set to an actual value
var box : String? = "Something"
var box: String?
print(box!)
var box: String? = "Something"
>>>
print(box!)
---
"Something"
var box: String?
>>>
print(box!)
---
// Ran in the same codeblock
var box1: String?
if let boxContents = box1 {
print(boxContents)
}
---
Nothing Prints
var box2: String? = "Something"
if let boxContents = box2 {
// Within a function
var box: String? = "Something"
guard let boxContents = box else { return }
print(boxContents)
---
"Something"
class Mom {
var _son: Son
init(son: Son) {
self._son = son
}
func tellJoshWhatToDo() {
self._son.washDishes()
self._son.cleanRoom()
protocol MomDelegate {
func washDishes()
func cleanRoom()
func walkNala()
}
class Son: MomDelegate {
func washDishes() {
print("Washing Dishes")
}
func cleanRoom() {
print("Cleaning Room")
}
let josh = Son()
let joshsMom = Mom(son: josh)
joshsMom.tellJoshWhatToDo()
---
"Washing Dishes"
"Cleaning Room"
"Walking Nala"
@josh-marasigan
josh-marasigan / SwiftMapExercise.swift
Last active April 28, 2018 15:22
Varying Swift syntax for map function
// 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